Category Archives: WordPress

How To Programmatically Add Or Update WordPress Custom Field

How can I add a WordPress post meta field and update the WordPress post meta field programmatically?

You can use function add_post_meta and update_post_meta to add or update the WordPress post meta field.

$post_id = 1;
$meta_key = "key";
$meta_value = "value";

//add new post meta field
add_post_meta($post_id, $meta_key, $meta_value);

//update the post meta field
update_post_meta( $post_id, $meta_key, $meta_value);

How To Programmatically Add WordPress Widget To Sidebar

How can I add WordPress widget to sidebar programmatically

 

You need to modify the value of option field – sidebars_widgets and widget option field (widget_[plugin_id]) like widget_rss or widget_text

    $counter = 1;  
    $active_widgets = get_option( 'sidebars_widgets' ); 

    // top-sidebar should be the id of the sidebar

    $active_widgets["top-sidebar"][] = 'rss-' . $counter;

    $rss_content[ $counter ] = array (  
        'title'        => 'Softwarehtec',  
        'url'          => 'http://softwarehtec.com/feeds',  
        'link'         => 'http://softwarehtec.com/questions',  
        'items'        => 15,  
        'show_summary' => 0,  
        'show_author'  => 1,  
        'show_date'    => 1,  
    ); 
    update_option( 'widget_rss', $rss_content );  
    update_option( 'sidebars_widgets', $active_widgets );  

How To Programmatically Activate WordPress Plugins

How can I programmatically activate WordPress plugins, e.g I would like to activate plugin A from plugin B

If you would like to activate WordPress plugins programmatically, you need to modify the value of “active_plugins” option.

    $current = get_option( 'active_plugins' );
    $plugin = plugin_basename( trim('akismet/akismet.php') );

    if ( !in_array( $plugin, $current ) ) {
        $current[] = $plugin;
        sort( $current );
        do_action( 'activate_plugin', trim( $plugin ) );
        update_option( 'active_plugins', $current );
        do_action( 'activate_' . trim( $plugin ) );
        do_action( 'activated_plugin', trim( $plugin) );
    }

How To Programmatically Create a WordPress Menu

How can I create a new WordPress menu programmatically and assign the menu item to the new menu

You can use WordPress native function wp_create_nav_menu to create a new menu and using wp_update_nav_menu_item to assign the menu items

$menuname = "New Menu";
$menu_exists = wp_get_nav_menu_object( $menuname );

if( !$menu_exists){
    $menu_id = wp_create_nav_menu($menuname);

    wp_update_nav_menu_item($menu_id, 0, array(
        'menu-item-title' =>  __('Home'),
        'menu-item-classes' => 'home',
        'menu-item-url' => home_url( '/' ), 
        'menu-item-status' => 'publish'));

}

How To Programmatically Create a WordPress Admin User

How can I create a WordPress user with admin role programmatically. So new user should have admin rights

To create a WordPress admin user programmatically, it’s similar with creating a new WordPress user but assign with Administrator role.

Firstly you need to check user exists or not by email and then Generating the password for the new user. Finally, Assign the Administrator role to new user

if( null == username_exists( $email_address ) ) {

  // Generate the password and create the user
  $password = wp_generate_password( 12, false );
  $user_id = wp_create_user( $email_address, $password, $email_address );

  // Set the nickname
  wp_update_user(
    array(
      'ID'          =>    $user_id,
      'nickname'    =>    $email_address
    )
  );

  // Set the role
  $user = new WP_User( $user_id );
  $user->set_role( 'administrator' );
}