How to add radio button Metabox in WordPress Custom and Default Post Types
add_action( 'add_meta_boxes', 'post_disable_enable' ); function post_disable_enable() { add_meta_box('post_disable_enable', 'Post Disable Enable', 'post_disable_enable_meta', 'post' ); } function post_disable_enable_meta( $post ) { wp_nonce_field( 'post_disable_enable', 'post_disable_enable_nonce' ); $value = get_post_meta( $post->ID, 'post_disable_enable', true ); echo '<input type="radio" name="post_disable_enable" value="disable" '; if($value=='disable') { echo 'checked'; } '> Disable<br/>'; echo '<input type="radio" name="post_disable_enable" value="enable" '; if($value=='enable') { echo 'checked'; } echo '> Enable<br/>'; } add_action( 'save_post', 'disable_enable_save_meta_box_data' ); function disable_enable_save_meta_box_data( $post_id ) { // Check if our nonce is set. if ( !isset( $_POST['post_disable_enable_nonce'] ) ) { return; } // Verify that the nonce is valid. if ( !wp_verify_nonce( $_POST['post_disable_enable_nonce'], 'post_disable_enable' ) ) { return; } // If this is an autosave, our form has not been submitted, so we don't want to do anything. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return; } // Check the user's permissions. if ( !current_user_can( 'edit_post', $post_id ) ) { return; } // Sanitize user input. $new_meta_value = ( isset( $_POST['post_disable_enable'] ) ? sanitize_html_class( $_POST['post_disable_enable'] ) : '' ); // Update the meta field in the database. update_post_meta( $post_id, 'post_disable_enable', $new_meta_value ); }
How to Change Admin URL Without Plugin
Step 1: Configuration define in wp-config.php
define('WP_ADMIN_DIR', 'private-area'); define( 'ADMIN_COOKIE_PATH', SITECOOKIEPATH . WP_ADMIN_DIR);
Step 2: Add a function in your theme functions.php file
add_filter('site_url', 'change_wpadmin_url', 10, 3); function change_wpadmin_url( $url, $path, $orig_scheme ) { $old_admin_url = array( "/(wp-admin)/"); $admin_dir = WP_ADMIN_DIR; $new_admin_url = array($admin_dir); return preg_replace( $old_admin_url, $new_admin_url, $url, 1); }
Step 3: Add a line to .htaccess file
RewriteRule ^private-area/(.*) wp-admin/$1?%{QUERY_STRING} [L]
Post Per Page on Search and Category Page
add_action( 'pre_get_posts', 'set_posts_per_page' ); function set_posts_per_page( $query ) { global $wp_the_query; if ( ( ! is_admin() ) && ( $query === $wp_the_query ) && ( $query->is_search() ) ) { $query->set( 'posts_per_page', 10 ); } elseif ( ( ! is_admin() ) && ( $query === $wp_the_query ) && ( $query->is_category() ) ) { $query->set( 'posts_per_page', 10 ); } return $query; }
How to Change Mail from and display name with a filter action
add_filter('wp_mail_from', 'new_mail_from'); add_filter('wp_mail_from_name', 'new_mail_from_name'); function new_mail_from($old) { return 'admin@example.com'; } function new_mail_from_name($old) { return 'DS Web Solutions'; }
How to Send HTML Emails From WordPress Using wp_mail Function
function dsweb_email_content_type(){ return "text/html"; } add_filter( 'wp_mail_content_type','dsweb_email_content_type' );
How to Disable Plugin Deactivation in Wordpress
add_filter( 'plugin_action_links', 'disable_plugin_deactivation', 10, 4 ); function disable_plugin_deactivation( $actions, $plugin_file, $plugin_data, $context ) { // Remove edit link for all if ( array_key_exists( 'edit', $actions ) ) unset( $actions['edit'] ); // Remove deactivate link for crucial plugins if ( array_key_exists( 'deactivate', $actions ) && in_array( $plugin_file, array( 'json-rest-api/plugin.php' ))) unset( $actions['deactivate'] ); return $actions; }
How to Change Preview Page link in Wordpress
function dswebsolutions_preview_link() { $site_url = site_url(); $mydir = $site_url.'/preview-page/'.get_the_ID(); return "$mydir"; } add_filter( 'preview_post_link', 'dswebsolutions_preview_link' );
How to Remove Query String from Static Resources in Wordpress
function remove_cssjs_ver( $src ) { if( strpos( $src, '?ver=' ) ) $src = remove_query_arg( 'ver', $src ); return $src; } add_filter( 'style_loader_src', 'remove_cssjs_ver', 10, 2 ); add_filter( 'script_loader_src', 'remove_cssjs_ver', 10, 2 );
How to Remove Shortcode in Wordpress
remove_action( 'wp_head', 'vh_shortocde' );
How to add Adding Registration fields to the Woocommerce Registration form
//Add Registration fields to the form add_filter( 'register_form', 'adding_custom_registration_fields' ); function adding_custom_registration_fields( ) { echo '<div class="form-row form-row-wide"><label for="reg_role">'.__('Role', 'woocommerce').' <span class="required">*</span></label>'; echo '<select name="wp_capabilities"> <option value="customer">Customer</option> <option value="designer">Designer</option> </select>'; echo '</div>'; }
//Validation registration form after submission using the filter registration_errors add_filter('registration_errors', 'registration_errors_validation', 10,3); function registration_errors_validation($reg_errors, $sanitized_user_login, $user_email) { global $woocommerce; extract($_POST); // extracting $_POST into separate variables if($wp_capabilities == '' ) { $woocommerce->add_error( __( 'Please, fill in all the required fields.', 'woocommerce' ) ); } return $reg_errors; }
//Updating use meta after registration successful registration add_action('woocommerce_created_customer','adding_extra_reg_fields'); function adding_extra_reg_fields($user_id) { extract($_POST); // update user role using wordpress function wp_update_user( array ('ID' => $user_id, 'role' => $wp_capabilities ) ) ; }
How to Add/Remove New Columns on wp-admin Products Listing Page
// ADD NEW COLUMN add_filter( 'manage_edit-product_columns', 'show_product_columns',15 ); function show_product_columns($columns){ //remove column unset($columns['sku']); unset($columns['product_tag']); unset($columns['product_type']); //add column $columns['author'] = __( 'Author'); return $columns; }
add_action( 'manage_product_posts_custom_column', 'product_column_author', 10, 2 ); function product_column_author( $column, $postid ) { if ( $column == 'author' ) { echo $postid; } }
How to add New Field in General Settings in WordPress
add_filter('admin_init', 'my_general_settings_on_dswebsolution'); function my_general_settings_on_dswebsolution() { register_setting('general', 'general_field_dswebsolution', 'esc_attr'); add_settings_field('general_field_dswebsolution', '<label for="general_field_dswebsolution">'.__('General Field Name' , 'general_field_dswebsolution' ).'</label>' , 'my_general_settings_dswebsolution_html', 'general'); } function my_general_settings_dswebsolution_html() { $value = get_option( 'general_field_dswebsolution', '' ); echo '<input type="text" id="general_field_dswebsolution" name="general_field_dswebsolution" value="' . $value . '" />'; }
How to Add a custom user role in WordPress
$result = add_role( 'designer', __('Designer' ), array( 'read' => true, // true allows this capability 'edit_posts' => true, // Allows user to edit their own posts 'edit_pages' => true, // Allows user to edit pages 'edit_others_posts' => true, // Allows user to edit others posts not just their own 'create_posts' => true, // Allows user to create new posts 'manage_categories' => true, // Allows user to manage post categories 'publish_posts' => true, // Allows the user to publish, otherwise posts stays in draft mode 'edit_themes' => false, // false denies this capability. User can't edit your theme 'install_plugins' => false, // User cant add new plugins 'update_plugin' => false, // User can't update any plugins 'update_core' => false // user cant perform core updates ) );
How to add Custom Status in Post/Custom Post Type in WordPress
function unapproved_post_status(){ register_post_status( 'unapproved', array( 'label' => _x( 'Unapproved', 'post' ), 'public' => true, 'exclude_from_search' => false, 'show_in_admin_all_list' => true, 'show_in_admin_status_list' => true, 'label_count' => _n_noop('Unapproved <span class="count">(%s)</span>', 'Unapproved <span class="count">(%s)</span>' ), ) ); } add_action( 'init', 'unapproved_post_status' );
How to change Woocommerce Currency Symbol
add_filter( 'woocommerce_currencies', 'add_inr_currency'); add_filter( 'woocommerce_currency_symbol', 'add_inr_currency_symbol'); function add_inr_currency( $currencies ) { $currencies['INR'] = 'INR'; return $currencies; } function add_inr_currency_symbol( $symbol ) { $currency = get_option( 'woocommerce_currency' ); switch( $currency ) { case 'INR': $symbol = 'Rs.'; break; } return $symbol; }
Output which theme template file a post/page is using in the header
add_action('wp_head', 'dsweb_show_template'); function dsweb_show_template() { global $template; print_r($template); }
How to Disable Search Feature in WordPress
function fb_filter_query( $query, $error = true ) { if (is_search()){ $query->is_search = false; $query->query_vars[s] = false; $query->query[s] = false; // to error if ( $error == true ) $query->is_404 = true; } } add_action( 'parse_query', 'fb_filter_query'); add_filter( 'get_search_form', create_function( '$a', "return null;" ) );
How to Install WordPress and setup Network
define('WP_ALLOW_MULTISITE', true);
How to Create and using custom hooks in WordPress
Step 1: Define custom hook
function dswebsolutions_hook() { do_action('dswebsolutions_hook'); }
Step 2: Tag your custom hook
Place the function call in the desired location within your theme template:
dswebsolutions_hook();
Step 3: Add your function
function hello_wordpress() { echo '<h1>Hello WordPress!</h1>'; } add_action('dswebsolutions_hook', 'hello_wordpress', 7);
How to create Google Maps Shortcode
//Google Maps Shortcode function fn_googleMaps($atts, $content = null) { extract(shortcode_atts(array( "width" => '640', "height" => '480', "src" => '' ), $atts)); return '<iframe width="'.$width.'" height="'.$height.'" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'.$src.'&output=embed"></iframe>'; } add_shortcode("googlemap", "fn_googleMaps");
[googlemap width="200" height="200" src="[url]"]