In 5 minute create your wordpress plugin using post shortcode plugin

Step 1 : create plugin-name.php file and add comment to create wordpress plugin. ( plugin must have below comment )

 

/**
 * Plugin Name: Post Shortcode Woocommerce
 * Plugin URI: 
 * Description: This plugin is used for display woocommerce product.
 * Version: 1.0
 * Author: Sachin Jadhav
 * Author URI: https://sachin8600.wordpress.com/
 * Requires at least: 3.8
 * Tested up to: 4.7
 *
 * Text Domain: wsw
 * Domain Path: /languages/
 *
 * @package psw
 * @author  Sachin Jadhav
 */ 

 

Step 2 : Add filter `post_shortcode_themes` to add our theme in post shortcode plugin

 

/**
* this filter add theme style
**/
add_filter('post_shortcode_themes', 'post_shortcode_themes_callback_woocommerce');
function post_shortcode_themes_callback_woocommerce( $atls ) {
	$atl = unserialize($atls);
    $atl["wsw"] = __( 'Woocommerce Style', 'pcs'  );
    $atl["gwsw"] = __( 'Grid Woocommerce Style', 'pcs'  );
    return serialize($atl); 
}

 

Step 3 : Add filter `post_shortcode_show_fields` to add our fields to post shortcode plugin

/**
* this filter add show fields
**/
add_filter('post_shortcode_show_fields', 'post_shortcode_show_fields_callback_woocommerce');
function post_shortcode_show_fields_callback_woocommerce( $asfs ) {
	$asf = unserialize($asfs);
    $asf["wooprice"] = __( 'Woocommerce Price', 'pcs'  );
    $asf["woocartbtn"] = __( 'Woocommerce Price + Add To Cart Button', 'pcs'  );
    return serialize($asf); 
}

 

Step 4 : Check dependency of plugin activation ( In my case woocommerce plugin activation also check , you can remove it if you not use woocommerce )

/**
* this hook use for check dependency on plugin activation
**/
register_activation_hook(__FILE__, 'pcs_check_dependancy_activate');
function pcs_check_dependancy_activate( $network_wide ) {
    $notices = "";
    $plugin_path = 'post-shortcode/post-shortcode.php';
    $plugin_version = '2.0.7'; 
    if(file_exists(WP_PLUGIN_DIR.'/'.$plugin_path)){

        if ( is_plugin_active( 'post-shortcode/post-shortcode.php' ) ) {

            $plugin_data = get_plugin_data( WP_PLUGIN_DIR.'/'.$plugin_path);
           // print_r($plugin_data);die;
            if( !version_compare( $plugin_data["Version"], $plugin_version, '>') ){
                $notices .= '

'.__( "Please update `Post Shortcode` Plugin.", "pcs" ).'

'; } }else{ $notices .= '

'.__( "Please active `Post Shortcode` Plugin.", "pcs" ).'

'; } }else{ $notices .= '

'.__( "Please install `Post Shortcode` Plugin.", "pcs" ).'

'; } /* you may remove below code if you not use woocommerce plugin */ /* start remove code */ $plugin_path = 'woocommerce/woocommerce.php'; $plugin_version = '1.0'; if(file_exists(WP_PLUGIN_DIR.'/'.$plugin_path)){ if ( is_plugin_active( 'post-shortcode/post-shortcode.php' ) ) { $plugin_data = get_plugin_data( WP_PLUGIN_DIR.'/'.$plugin_path); // print_r($plugin_data);die; if( !version_compare( $plugin_data["Version"], $plugin_version, '>') ){ $notices .= '

'.__( "Please update `Woocommerce` plugin.", "pcs" ).'

'; } }else{ $notices .= '

'.__( "Please active `Woocommerce` plugin.", "pcs" ).'

'; } }else{ $notices .= '

'.__( "Please install `Woocommerce` plugin.", "pcs" ).'

'; } /* stop remove code */ if($notices){ $notices .= '

'.__( "`Post Shortcode Woocommerce` plugin deactivated.", "pcs" ).'

'; update_option('psw_plugin_notices', $notices); set_transient( 'psw_plugin_notices', true, 5 ); } } /** * this action use for add admin notice **/ add_action( 'admin_notices', 'psw_plugin_notices_print' ); function psw_plugin_notices_print(){ if( get_transient( 'psw_plugin_notices' ) ){ if(get_option("psw_plugin_notices")){ echo get_option("psw_plugin_notices"); deactivate_plugins( 'post-shortcode-woocommerce/post-shortcode-woocommerce.php' ); } delete_option( 'psw_plugin_notices' ); delete_transient( 'psw_plugin_notices' ); } }

 

Step : Add your customize code in your theme function ( In above case in theme filter there are two theme one is wsw so that function name of wsw become `post_shortcode_callback_for_wsw()` and second is gwsw so that function name become `post_shortcode_callback_for_gwsw()` )

 

/* plugin theme function of gwsw theme */
function post_shortcode_callback_for_gwsw($a){
        global $pcs;
        $app = array();
        $aft = $pcs->aft;
        $a = array_filter($a,"sanitize_text_field");
        extract($a);
        if(isset($posttype) && !empty($posttype)){
            $args["post_type"] = explode(",", $posttype); 
        }else{
            $args["post_type"] = explode(",", "post"); 
        }
        $showfield = (isset($showfield) && !empty($showfield)) ? $showfield : "title,thumbnail,excerpt";
        $ashowfield =explode(",", $showfield);
        $args["post_status"] = array( "publish" );
        $args["posts_per_page"] = (isset($postcount) && !empty($postcount)) ? $postcount : 3;
        $paged = (isset($paged) && !empty($paged)) ? $paged : 1;
        $taxrel = (isset($taxrel) && !empty($taxrel)) ? $taxrel : "OR";
        $args["paged"] = (int) $paged;
        $pagedvar = (isset($pagedvar) && !empty($pagedvar)) ? $pagedvar : "paged";
        if(in_array("pagination", $ashowfield)){
            $args["paged"] = 1;
            if(isset($_GET["pagedvar"]) && $_GET["pagedvar"] == $pagedvar ){
                $args["paged"] =  ( get_query_var("paged") == 0 ) ? 1 : get_query_var("paged");
            }
        }
        if(!empty($categories)):
            $acat = explode(",", $categories);
            if(!empty($acat)):
                foreach ($acat as $catkey => $catvalue) {
                    $actt = explode("$", $catvalue);
                    $ctkey = $actt[0];
                    $ctvalue = $actt[1];
                    $afctt[$ctkey][] = $ctvalue;
                }
            endif;
            if(!empty($afctt)):
                foreach ($afctt as $afckey => $afcvalue) {
                    $tax[] = array(
                            "taxonomy" => $afckey,
                            "field"    => "slug",
                            "terms"    => $afcvalue,
                        );
                }
            endif;
            $tax_query = array(
                    "relation" => $taxrel,
                    $tax
                );
            $args["tax_query"] = $tax_query;
        endif;
        $orderby = (isset($orderby) && !empty($orderby)) ? $orderby : "modified";
        $args["orderby"] = $orderby;
        $order = (isset($order) && !empty($order)) ? $order : "DESC";
    	$args["order"]   = $order;
        $tsize = (isset($tsize) && !empty($tsize)) ? $tsize : "thumbnail";
        $excerptl = (isset($excerptl) && !empty($excerptl)) ? $excerptl : 55;
        foreach ($aft as $aftvalue => $aftkey ) {
            if(isset($$aftvalue) && !empty($$aftvalue)){
                $app = explode( ",", $$aftvalue );
                $args[$aftvalue] = $app;
            }
        }
    	query_posts($args);
    	// The Loop
        ob_start();
        if ( have_posts() ) :   
            if($template == "gwsw") { $template =  " gcore "; } //we use plugin css of `gcore` theme you can remove this line add css in plugin backend css editor in our case `gwsw` is class print we write css using .gwsw{} class   ?>
            
" >
"; the_post_thumbnail($tsize, array( "class" => "img-responsive" ) ); echo ""; }?>

"; the_title(); echo "";?>

get_price_html(); endif; if(in_array("woocartbtn", $ashowfield)): echo do_shortcode("[add_to_cart id=".get_the_ID()."]"); endif; ?>
<?php endif; ?> <?php if(in_array("content", $ashowfield)): ?>
<?php endif; ?> <?php if(!empty($customfield)): $acustomfield = explode(",", $customfield); $pid = get_the_ID(); foreach ($acustomfield as $ackey => $acvalue) { $actxt = ""; $actxt = get_post_meta($pid,$acvalue,true); if(!empty($actxt)) echo "<span class=\'pcs-reset pcs-cust-field ".$acvalue."\'>".$actxt."</span>"; } endif; ?>
%2$s"; $dateformat = get_option("date_format"); _e( "On  ", "pcs" ) ?>" rel="bookmark"> " rel="author">
<?php if(in_array("readme", $ashowfield)): ?> <a class="pcs-rm pcs-reset" href="<?php echo $lnk; ?>"><?php echo $readmoretitle; ?></a> <?php endif; ?> </div> </div> <?php endwhile; ?> </div> <?php /** * @version 2.0.7 * pagination code for query post */ if(in_array("pagination", $ashowfield)){ ?> <nav class="pcs-pagination pcs-reset" > <?php global $wp_query; $big = 999999999; // need an unlikely integer $asearch = array( $big, "#038;" ); $areplace = array( "%#%", "&" ); echo paginate_links( array( "base" => str_replace( $asearch, $areplace, esc_url( get_pagenum_link( $big ) ) ), "format" => "?paged=%#%", "current" => max( 1, get_query_var("paged") ), "total" => $wp_query->max_num_pages, "type" => "list", "prev_text" => "&laquo;", "next_text" => "&raquo;", "add_args" => array( "pagedvar" => $pagedvar), ) ); ?> </nav> <?php } endif; $output = ob_get_contents(); ob_end_clean(); // Reset Query wp_reset_query(); return $output; } /* plugin theme function of wsw theme */ function post_shortcode_callback_for_wsw($a){ global $pcs; $app = array(); $aft = $pcs->aft; $a = array_filter($a,"sanitize_text_field"); extract($a); if(isset($posttype) && !empty($posttype)){ $args["post_type"] = explode(",", $posttype); }else{ $args["post_type"] = explode(",", "post"); } $showfield = (isset($showfield) && !empty($showfield)) ? $showfield : "title,thumbnail,excerpt"; $ashowfield =explode(",", $showfield); $args["post_status"] = array( "publish" ); $args["posts_per_page"] = (isset($postcount) && !empty($postcount)) ? $postcount : 3; $paged = (isset($paged) && !empty($paged)) ? $paged : 1; $taxrel = (isset($taxrel) && !empty($taxrel)) ? $taxrel : "OR"; $args["paged"] = (int) $paged; $pagedvar = (isset($pagedvar) && !empty($pagedvar)) ? $pagedvar : "paged"; if(in_array("pagination", $ashowfield)){ $args["paged"] = 1; if(isset($_GET["pagedvar"]) && $_GET["pagedvar"] == $pagedvar ){ $args["paged"] = ( get_query_var("paged") == 0 ) ? 1 : get_query_var("paged"); } } if(!empty($categories)): $acat = explode(",", $categories); if(!empty($acat)): foreach ($acat as $catkey => $catvalue) { $actt = explode("$", $catvalue); $ctkey = $actt[0]; $ctvalue = $actt[1]; $afctt[$ctkey][] = $ctvalue; } endif; if(!empty($afctt)): foreach ($afctt as $afckey => $afcvalue) { $tax[] = array( "taxonomy" => $afckey, "field" => "slug", "terms" => $afcvalue, ); } endif; $tax_query = array( "relation" => $taxrel, $tax ); $args["tax_query"] = $tax_query; endif; $orderby = (isset($orderby) && !empty($orderby)) ? $orderby : "modified"; $args["orderby"] = $orderby; $order = (isset($order) && !empty($order)) ? $order : "DESC"; $args["order"] = $order; $tsize = (isset($tsize) && !empty($tsize)) ? $tsize : "thumbnail"; $excerptl = (isset($excerptl) && !empty($excerptl)) ? $excerptl : 55; foreach ($aft as $aftvalue => $aftkey ) { if(isset($$aftvalue) && !empty($$aftvalue)){ $app = explode( ",", $$aftvalue ); $args[$aftvalue] = $app; } } query_posts($args); // The Loop ob_start(); if ( have_posts() ) : if($template == "wsw") { $template = " core "; } //we use plugin css of `core` theme you can remove this line add css in plugin backend css editor in our case `wsw` is class print we write css using .wsw{} class ?>
" >
"; the_post_thumbnail($tsize, array( "class" => "img-responsive" ) ); echo ""; }?>

"; the_title(); echo "";?>

get_price_html(); endif; if(in_array("woocartbtn", $ashowfield)): echo do_shortcode("[add_to_cart id=".get_the_ID()."]"); endif; ?>
<?php endif; ?> <?php if(in_array("content", $ashowfield)): ?>
<?php endif; ?> <?php if(!empty($customfield)): $acustomfield = explode(",", $customfield); $pid = get_the_ID(); foreach ($acustomfield as $ackey => $acvalue) { $actxt = ""; $actxt = get_post_meta($pid,$acvalue,true); if(!empty($actxt)) echo "<span class=\'pcs-reset pcs-cust-field ".$acvalue."\'>".$actxt."</span>"; } endif; ?>
%2$s"; $dateformat = get_option("date_format"); _e( "On  ", "pcs" ) ?>" rel="bookmark"> " rel="author">
<?php if(in_array("readme", $ashowfield)): ?> <a class="pcs-rm pcs-reset" href="<?php echo $lnk; ?>"><?php echo $readmoretitle; ?></a> <?php endif; ?> </div> </div> <?php endwhile; ?> </div> <?php /** * @version 2.0.7 * pagination code for query post */ if(in_array("pagination", $ashowfield)){ ?> <nav class="pcs-pagination pcs-reset" > <?php global $wp_query; $big = 999999999; // need an unlikely integer $asearch = array( $big, "#038;" ); $areplace = array( "%#%", "&" ); echo paginate_links( array( "base" => str_replace( $asearch, $areplace, esc_url( get_pagenum_link( $big ) ) ), "format" => "?paged=%#%", "current" => max( 1, get_query_var("paged") ), "total" => $wp_query->max_num_pages, "type" => "list", "prev_text" => "&laquo;", "next_text" => "&raquo;", "add_args" => array( "pagedvar" => $pagedvar), ) ); ?> </nav> <?php } endif; $output = ob_get_contents(); ob_end_clean(); // Reset Query wp_reset_query(); return $output; }

 

Sample Code : Click here

 

Thanks to visits

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s