Tag: WordPress

PHP Tricks

  1. To see the var_dump output with decoration see below –
echo '<pre>', var_dump( $style_post ), '</pre>';

Conditional Script Loading for Shotcode

In the functions.php file or in your plugin file write the code below –

[php]

/**
* Check if it should load frontend scripts
*
* @return mixed|void
*/
private function should_load_scripts() {
global $post;
$load_scripts = is_active_widget( false, false, ‘wpl_logo_carousel_pro_widget_content’, true ) || ( is_a( $post, ‘WP_Post’ ) && has_shortcode( $post->post_content, ‘logo-carousel-pro’ ) );

return apply_filters( ‘logo_carousel_pro_load_scripts’, $load_scripts );
}

/**
* Plugin Scripts and Styles
*
*/
function public_scripts() {
if ( $this->should_load_scripts() ) {
wp_enqueue_script( ‘jquery-isotope-min-js’, MY_URL . ‘assets/js/jquery.isotope.min.js’, array( ‘jquery’ ), $this->version, false );
}
}

[/php]

How to Set a default Title for the custom post?

[php]
function wpl_lc_add_custom_title( $data, $postarr ) {
if($data[‘post_type’] == ‘wpl_lcp_shortcodes’) {
if(empty($data[‘post_title’])) {
$data[‘post_title’] = ‘Untitled Carousel’;
}
}
return $data;
}
add_filter(‘wp_insert_post_data’, ‘wpl_lc_add_custom_title’, 10, 2 );
[/php]

Now use the function
[php]
the_title();
[/php]

How to a Add Custom Meta Box for Multiple Post Type

[php]
add_action( ‘add_meta_boxes’, ‘myplugin_add_custom_box’ );

function myplugin_add_custom_box() {

add_meta_box( ‘myplugin_sectionid’, __( ‘My Post Section Title’, ‘myplugin_textdomain’ ), ‘myplugin_inner_custom_box’, ‘post’ );

add_meta_box( ‘myplugin_sectionid’, __( ‘My Post Section Title’, ‘myplugin_textdomain’ ), ‘myplugin_inner_custom_box’, ‘page’ );

}
[/php]

See: Stackexchange , Codex

How to show image from CodeStar Metabox

The non-repeatable image field creation:

[php]
$options[] = array(
‘id’ => ‘_custom_category_options’,
‘post_type’ => ‘category’, // or array( ‘category’, ‘post_tag’ )
‘context’ => ‘normal’,
‘priority’ => ‘default’,
‘sections’ => array(
// begin: a section
array(
‘name’ => ‘section_1’,
‘title’ => ‘Section 1’,
‘icon’ => ‘fa fa-cog’,

// begin: fields
‘fields’ => array(

// a field
array(
‘id’ => ‘sponsors_image’,
‘type’ => ‘image’,
‘title’ => ‘Clients Logo’,
),

), // end: fields
), // end: a section

),
);
[/php]
02. Calling the image on front-end:
[php]