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; }
Comment Like and Dislike Function in Wordpess
// Create Table in your Databse SQL CREATE TABLE custom_comment_likes( id INT NOT NULL AUTO_INCREMENT, comment_id INT NOT NULL, ip_address VARCHAR (255), status INT NOT NULL, PRIMARY KEY (id) );
function dsweb_comment_like_dislike(){ if (!empty($_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip = $_SERVER['REMOTE_ADDR']; } global $wpdb; $like_count = $wpdb->get_var( "SELECT * FROM custom_comment_likes WHERE `ip_address` = '".$ip."' AND `comment_id` = '".$_POST[id]."'"); $count = $wpdb->num_rows; if(!$count > 0){ $wpdb->insert( 'custom_comment_likes', array( 'ip_address' => $ip, 'comment_id' => $_POST[id] , 'status' => $_POST[status]), array( '%s', '%d' ,'%d') ); }else{ $wpdb->update('custom_comment_likes', array( 'status' => $_POST[status]), array( 'comment_id' => $_POST[id] ,'ip_address' => $ip), array('%d' ),array( '%d','%s' ) ); } $like_query = $wpdb->get_results("SELECT * FROM custom_comment_likes WHERE `comment_id` = '".$_POST[id]."' AND `status` = '1'"); $like = $wpdb->num_rows; $dislike_query = $wpdb->get_results("SELECT * FROM custom_comment_likes WHERE `comment_id` = '".$_POST[id]."' AND `status` = '0'"); $dislike = $wpdb->num_rows; $arg['success']['like'] = $like; $arg['success']['dislike'] = $dislike; return $arg; }
How to get previous and next post of given post id in Wordpress
function getPreviousNext($currenPostID){ $postids = array(); global $post; $post = get_post($currenPostID); $previous_post = get_previous_post(); $next_post = get_next_post(); $nextPost = get_post($next_post->ID); $prevPost = get_post($previous_post->ID); $postids["nextSlug"] = $prevPost->post_name; $postids["prevSlug"] = $nextPost->post_name; $postids["prevlink"] = $next_post->ID; $postids["nextlink"] = $previous_post->ID; return $postids; }
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 get terms in Wordpress
function get_year_taxonomy_child() { $values = get_terms('events-years'); foreach ($values as $value) { $categories[] = array('name'=>$value->name,'slug'=>$value->slug); } $output = ''; $output .= '<div id="dd11" class="wrapper-dropdown-11 dropdown_list" tabindex="4"> <span>'.__('All Years','vh').'</span> <ul class="dropdown">'; foreach ( $categories as $cat ) { $output .= '<li><a href="#">'.$cat['name'].'</a><input type="hidden" value="'.$cat['slug'].'"></li>'; } $output .= '</ul> </div>'; return $output; }
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 get User Role in WordPress
function get_user_role() { global $current_user; $user_roles = $current_user->roles; $user_role = array_shift($user_roles); return $user_role; }
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); }