Tag Archives: plugin

Create Plugin From Custom Post Type

1. Create folder with name “rar-portfolio” in plugins folder.
2. Create a file name with “portfolio.php”.
3. Add below code into “portfolio.php”.

Note: Create custom post type on plugin activation with short code

We used this custom post type : Create and display custom post type with Texonomy, Metabox and Tag


<?php
/**
 * Plugin Name: RAR Portfolio
 * Plugin URI: http://rarthemes.com
 * Description: Create custom post type on plugin activation with short code
 * Version: 1.0.0
 * Author: Ravi Radadiya
 * Author URI: http://rarthemes.com
 * License: GPL2
 */
 
/***** Start custom post type - Portfolios *****/
function portfolios_custom_init() {
$labels = array(
'name' => __('Portfolios','themename'),
'singular_name' => __('portfolios','themename'),
'add_new' => __('Add New','themename'),
'add_new_item' => __('Add New Portfolio','themename'),
'edit_item' => __('Edit Portfolio','themename'),
'new_item' => __('New Portfolio','themename'),
'all_items' => __('All Portfolios','themename'),
'view_item' => __('View Portfolios','themename'),
'search_items' => __('Search Portfolio','themename'),
'not_found' => __('No Portfolios found','themename'),
'not_found_in_trash' => __('No Portfolios found in Trash','themename'),
'parent_item_colon' => '',
'menu_name' => __('Portfolios','themename')
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'portfolios' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'editor','thumbnail')
 
);
register_post_type( 'portfolios', $args );
register_taxonomy("portfolio-categories",
array("portfolios"),
array("hierarchical" => true,
'show_admin_column'   => true,
"label" => "Portfolio Categories",
"singular_label" => "Portfolio Categories",
"rewrite" => array( 'slug' => 'portfolio_category' ,
 
)));
 
register_taxonomy("portfolio-tag",
array("portfolios"),
array("hierarchical" => false,
'show_admin_column'   => true,
"label" => "Portfolio Tag",
"singular_label" => "Portfolio tag",
"rewrite" => array( 'slug' => 'portfolio_tag' ,
 
)));
 
}
add_action( 'init', 'portfolios_custom_init' );
 
/*****  Custom Meta Box - portfolios *****/
 
add_action( 'add_meta_boxes', 'meta_box_portfolio' );
function meta_box_portfolio()
{
add_meta_box( 'meta_box_portfolio_id', 'Enter Portfolio info', 'portfolio_website_info', 'portfolios', 'normal', 'high' );
}
function portfolio_website_info( $post )
{
$values = get_post_custom( $post->ID );
$portfolio_url = isset( $values['portfolio_url'] ) ? esc_attr( $values['portfolio_url'][0] ) : '';
$portfolio_text = isset( $values['portfolio_text'] ) ? esc_attr( $values['portfolio_text'][0] ) : '';
$portfolio_image = isset( $values['portfolio_image'] ) ? esc_attr( $values['portfolio_image'][0] ) : '';
 
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>

<p>Portfolio URL</p>
<input type="text" name="portfolio_url" value="<?php echo esc_url( $portfolio_url ); ?>" class="widefat" placeholder="Portfolio Url" />
<p>Portfolio Text</p>
<input type="text" name="portfolio_text" value="<?php echo esc_attr( $portfolio_text ); ?>" class="widefat" placeholder="Portfolio Text" />
<p>Portfolio Image</p>
<input id=""  type="text" class=" mytheme_media_url upload" name="portfolio_image" value="<?php echo esc_attr( $portfolio_image ); ?>" placeholder="No file chosen"  />
<?php if(!empty($instance['image'])) { ?>
<img src="<?php echo $instance['image']; ?>"  />
<?php } ?>
<input id="mytheme_image_uploader"  class="button mytheme_media_upload" type="button" value="Upload" />
<?php
} 
add_action( 'save_post', 'portfolio_meta_box_save' );
function portfolio_meta_box_save( $post_id )
{
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
if( isset( $_POST['portfolio_url'] ) ) update_post_meta( $post_id, 'portfolio_url', esc_url( $_POST['portfolio_url'] ) );
if( isset( $_POST['portfolio_text'] ) ) update_post_meta( $post_id, 'portfolio_text', esc_attr( $_POST['portfolio_text'] ) );
if( isset( $_POST['portfolio_image'] ) ) update_post_meta( $post_id, 'portfolio_image', esc_attr( $_POST['portfolio_image'] ) );
}
/*** end portfolio post type ***/

/* Register activation hook */
function portfolio_plugin_activation() {
    portfolios_custom_init();
   // flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'portfolio_plugin_activation' );

/* Display posts */

function display_portfolio(){
	?>
<div class="row">
  <?php
$legal_portfolio_args = array(
'paged' => $paged,
'orderby' => 'post_date',
'order' => 'ASC',
'post_type' => 'portfolios',
'post_status' => 'publish'
);
$legal_portfolio = new WP_Query( $legal_portfolio_args );
while ( $legal_portfolio->have_posts() ) { $legal_portfolio->the_post();
?>
  <div class="col-md-6 col-sm-6 attorneys">
    <div class="our-section-box">
      <?php if ( has_post_thumbnail() ) { ?>
      <a href="<?php the_permalink(); ?>" target="_blank">
      <div class="col-md-12 no-padding our-box-left">
        <?php the_post_thumbnail( 'legal-ourteam-thumbnail-image', array( 'alt' => get_the_title(), 'class' => 'img-responsive') ); ?>
      </div>
      </a>
      <?php } ?>
      <div class="col-md-12 our-box-right">
        <h4>
          <?php the_title(); ?>
        </h4>
        <?php the_content(); ?>
        <?php
echo 'URL: '.  get_post_meta( get_the_ID(), 'portfolio_url', true );
echo '<br>text: '. get_post_meta( get_the_ID(), 'portfolio_text', true );
echo '<br>Image: '. get_post_meta( get_the_ID(), 'portfolio_image', true );
echo '<br>Category: '; foreach((get_the_terms($post->ID, 'portfolio-categories')) as $term) { echo $term->name. ' ,'  ; }
echo '<br>tag: '; foreach((get_the_terms($post->ID, 'portfolio-tag')) as $term) { echo $term->name. ' ,'  ; }
?>
      </div>
    </div>
  </div>
  <?php } ?>
</div>
<?php
}

/* Add short code */

add_shortcode('rar_portfolio', 'portfolio_shortcode');
function portfolio_shortcode(){ 
	return display_portfolio();
}

Display plugin on frontend side if is active

/* if booking plugin is active */
function legalattorneys_shortcode(){
include_once( ABSPATH . ‘wp-admin/includes/plugin.php’ );
if(is_plugin_active(‘fastbook-responsive-appointment-booking-and-scheduling-system/fastbook.php’)) {
echo do_shortcode(‘[fastbook]’);
}
}

/* Display any where */

<?php legalattorneys_shortcode(); ?>

Enque script for plugin (backend)

function load_admin_style()
{
wp_enqueue_style( ‘admin_css’, ATTENDANCE_URL . ‘css/mycustomestyle.css’, false );
wp_enqueue_style( ‘datepicker1’, ATTENDANCE_URL . ‘datepicker/jquery-ui.css’, array());
wp_enqueue_script( ‘datepicker2’, ATTENDANCE_URL . ‘datepicker/jquery-1.10.2.js’, array());
wp_enqueue_script( ‘datepicker3’, ATTENDANCE_URL . ‘datepicker/jquery-ui.js’, array());
wp_enqueue_script( ‘mycustomscript’, ATTENDANCE_URL . ‘js/mycustomescript.js’, array());
}
add_action( ‘admin_enqueue_scripts’, ‘load_admin_style’ );

%d bloggers like this: