Posts

Showing posts with the label wordpress

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' );            } }

How to create custom sidebar in wordpress

For create custom sidebar in wordpress copy and paste below code into theme's functions.php file or into your custom plugins file. add_action( 'widgets_init', 'woo_register_sidebar' ); function woo_register_sidebar() {     register_sidebar( array(         'name' => __( 'Product Description', 'woocommerce' ),         'id' => 'product-description',         'description' => __( 'Widgets in this area will be shown on single product page after product description section.', 'woocommerce' ),         'before_widget' => '<div class="product_description_widget">',     'after_widget'  => '</div>',     'before_title'  => '<h2 class="widgettitle">',     'after_title'   => '</h2>',     ) ); } For access custom sidebar ...

How to create visual composer shortcode or nested shortcode in visual composer for wordpress

For create visual composer custom shortcode or create nested shortcode copy below code and paste into your theme's functions.php file or custom plugin file and change according to your need. <?php class WPBakeryShortCode_michelle_item_lists  extends WPBakeryShortCodesContainer {} //Register "container" content element. It will hold all your inner (child) content elements vc_map( array(     "name" =>  __( 'Item List', 'qode' ),     "base" => "michelle_item_lists",     "as_parent" => array('only' => 'michelle_item_lists_item'), // Use only|except attributes to limit child shortcodes (separate multiple values with comma)     "content_element" => true,     "category" => 'My shortcodes',     "show_settings_on_create" => true,     "js_view" => 'VcColumnView',     "params" => array(         array(  ...

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 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...

Custom password reset functionality with form for wordpress

For add a custom password reset functionality in your wordpress site or blog just copy and paste below code into new template "password reset" or whatever you want to name it. and put into your theme's folder.. <?php /* Template Name: Password Reset Template */ global $wpdb, $user_ID; function tg_validate_url() {     global $post;     $page_url = esc_url(get_permalink( $post->ID ));     $urlget = strpos($page_url, "?");     if ($urlget === false) {         $concate = "?";     } else {         $concate = "&";     }     return $page_url.$concate; } if (!$user_ID) { //block logged in users     if(isset($_POST['action']) && $_POST['action']=='save_pw_reset')     {         if ( !wp_verify_nonce( $_POST['save_pwd_nonce'], "save_reset_password"))...