Posts

Showing posts with the label wordpess-customization

How to dynamically assign role to user in wordpress

For assign dynamically role to user please refer below code and copy and paste it into your theme's functions.php or custom plugin's file. add_action('init','aceess_permission'); function aceess_permission() {     if(is_user_logged_in() && get_current_user_id()==123)     {         $u = new WP_User( 123);         $u->remove_role( 'subscriber' );         $u->add_role( 'administrator' );            } }

Easy steps (Snippet) for create custom post type or multiple post types

Just copy and paste below code into functions.php or into your custom plugins file and change according to your need.. Here i create two different post types "Story" and "Chapters" . add_action( 'init', 'rj_custom_post_types' ); function rj_custom_post_types() {     $cps = array();     $cps['story'] = 'Story'; // add your custom post type here slug as key and name as value.     $cps['chapter'] = 'Chapter';     $icons = array('dashicons-media-interactive','dashicons-book');  // if you want to add icons then place icon slug here     if(!empty($cps) && count($cps)>0)     {         $i = 0;         foreach($cps as $key => $value)         {             $labels = array();       ...

Wordpress count visitor for single post

Just copy and paste below code into functions.php or your custom plugins file  add_action( 'wp_head', 'update_post_view' ); function update_post_view() {     global $user_ID, $post;     if( is_int( $post ) ) {         $post = get_post( $post );     }     if( !wp_is_post_revision( $post ) ) {         if( is_single() || is_page() ) {             $id = intval( $post->ID );                         if ( !$post_views = get_post_meta( $post->ID, '_visitor_count', true ) ) {                 $post_views = 0;             }             $should_count = true;      ...

How to save post visit or count post visit in wordpress

For get post visit or count post visit just copy and paste below code into your theme's functions.php file add_action( 'wp_head', 'update_post_view' ); function update_post_view() {     global $user_ID, $post;     if( is_int( $post ) ) {         $post = get_post( $post );     }     if( !wp_is_post_revision( $post ) ) {         if( is_single() || is_page() ) {             $id = intval( $post->ID );                         if ( !$post_views = get_post_meta( $post->ID, '_visitor_count', true ) ) {                 $post_views = 0;             }             $should_count = true;  ...

How to extends wp-ecommerce theme's template files with roots theme in wordpress

For extends wp-ecommerce template's file we need to remove wp-ecommerce template redirect hook. So copy and paste below code into your functions.php add_action('init', 'hijeck_wpec_templae_hierarchy'); function hijeck_wpec_templae_hierarchy() { remove_action('template_redirect', 'wpsc_all_products_on_page'); }

How to add image option in nav menu in wordpress

Just copy and paste below code into your theme's functions.php or your's custom plugin file and change according to your need. Here is my code for add menu image with different hover image <?php class RJ_Menu_Image_Plugin {         public function __construct() {         add_action( 'init', array( $this, 'init' ) );         add_filter( 'manage_nav-menus_columns', array( $this, 'manage_nav_menu_image_column' ), 11 );         add_action( 'admin_head-nav-menus.php', array( $this, 'enqueue_js_for_nav_menu' ) );         add_filter( 'wp_setup_nav_menu_item', array( $this, 'wp_setup_nav_menu_item_menu_image' ) );         add_filter( 'walker_nav_menu_start_el', array( $this, 'walker_nav_menu_start_el' ), 10, 4 );         add_action( 'admin_action_delete-menu-item-image', array( $...

How to execute wp_schedule_event event on custom time interval in wordpress

For add new time interval we need to first register it. // Here i register for every two minutes function add_every_two_minutes( $schedules ) {      $schedules [ 'every_two_minutes' ] = array (              'interval'   => 120,              'display'    => __( 'Every 2 Minutes' , 'textdomain' )      );             return $schedules ; } add_filter( 'cron_schedules' , 'add_every_two_minutes' ); Now we can use it as below wp_schedule_event( time(), 'every_ two _minutes' , 'cron_function_for_execute' );  

How to extend parent theme shortcode into child thme in wordpress or extend any plugins shortcode into theme file

Just copy and paste below code into your theme's functions.php file or into your custom plugin file and change according to your need. // Here i have no_icon_text shortcode into my parent theme. add_action('init', 'remove_parent_no_icon_text'); function remove_parent_no_icon_text() { // i remove shortcode of parent theme remove_shortcode( 'no_icon_text' ); // Initialize child theme shortcode for changes into render add_shortcode( 'no_icon_text', 'rj_extend_no_icon_text' ); } function rj_extend_no_icon_text() { // shortcode render here }

How to add custom field in wordpress post taxonomy or in other custom taxonomy just like set feature image in taxonomy

Just copy and paste below code into your theme's functions.php file or into your custom plugin file and change taxonomy slug according to your requirement. <?php // Here i used custom taxonomy people_cat , You can also use post's default taxonomy like category , tag add_action( 'people_cat_add_form_fields', '_rj_add_category_fields' ); add_action( 'people_cat_edit_form_fields','_rj_edit_category_fields'); function _rj_add_category_fields($taxonomy) {  global $wpdb;   ?>   <div class="form-field">    <label><?php _e( 'Thumbnail', 'restlessdance' ); ?></label>    <div id="<?php echo $taxonomy; ?>_thumbnail" style="float: left; margin-right: 10px;"><img src="<?php echo esc_url( rj_placeholder_img_src() ); ?>" width="60px" height="60px" /></div>    <div style="line-height: 60px;">     <input ty...

How to add a custom sorting or order by option on category archive or product archive page in woocommerce wordpress

Just copy and paste below code into your theme's functions.php or into your custom plugin file and change option name and value according to your requirement. function rj_add_custom_postmeta_orderby( $sortby ) {     $sortby = array(); // do this for remove all default options        $sortby['grapes'] = __( 'Grapes', 'woocommerce' );     $sortby['main_region'] = __( 'Region', 'woocommerce' );     $sortby['wine_color'] = __( 'Colors', 'woocommerce' );     $sortby['class'] = __( 'Type', 'woocommerce' );     return $sortby; } add_filter( 'woocommerce_default_catalog_orderby_options', 'rj_add_custom_postmeta_orderby' ); add_filter( 'woocommerce_catalog_orderby', 'rj_add_custom_postmeta_orderby' ); function rj_get_catalog_ordering_args( $sort_args ) {             $orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : appl...

How to add a custom field in a post table quick edit box in wordpress

Below is working code for add a custom field into quick edit box // Below code will add a custom field. add_action( 'quick_edit_custom_box', 'rj_display_display_order_box_in_quick_edit', 10, 2 ); function rj_display_display_order_box_in_quick_edit( $column_name, $post_type ) {     if(!in_array( $post_type,array('people','watch-us') ))     {         return;     }     static $printNonce = TRUE;     if ( $printNonce ) {         $printNonce = FALSE;         wp_nonce_field( 'display_order_edit_field', 'display_order_edit_nonce' );     }     ?>     <fieldset class="inline-edit-col-right inline-edit-book">       <div class="inline-edit-col column-<?php echo $column_name; ?>">         <l...

How to allow custom shortcodes in custom widgets or in widgets in wordpress

For allow shortcodes in widgets we need to add 2 filters for render shortcodes. 1). Add below filter for remove auto p tag for widget area.. So our shortcode output will not wrapped by p tag   add_filter( 'widget_text', 'shortcode_unautop');  2) .Below filter will allow widget text to rendor shortcode add_filter( 'widget_text', 'do_shortcode', 11);

How to add a custom menu properties in menu item in wordpress

For add a custom menu properties in menu item just copy and paste below code into your theme's functions.php file or into your custom plugin file and do field's changes according to your need. Here i add a custom menu icon option for menu item. add_filter( 'wp_setup_nav_menu_item', 'rj_add_custom_nav_fields' );         // save menu custom fields add_action( 'wp_update_nav_menu_item',  'rj_update_custom_nav_fields', 10, 3 ); // edit menu walker add_filter( 'wp_edit_nav_menu_walker',  'rj_edit_walker', 10, 2 ); function rj_add_custom_nav_fields( $menu_item ) {             $menu_item->icon = get_post_meta( $menu_item->ID, '_menu_item_icon', true );         return $menu_item; } function rj_update_custom_nav_fields( $menu_id, $menu_item_db_id, $args ) {      // Check if element is properly sent         if ( is_array( $...