How to check if current post have specific shortcode in Wordpress

Shortcode is the one of powerful feature in Wordpress which provides so much flexibility to enhance functionalities. Sometime you need to detect if current post/page have already specific shortcode.

Here is the following two snippets which you could use for check specific shprtcode into your shortcode callback function.

Snippet -1(Using has_shortcode() function)

<?php 

add_shortcode('shortcode_name','shortcode_callback');

function shortcode_callback($atts){

global $post;

$shortcode_feature = ' ';

if (has_shortcode( $post->content'shortcode_name' )){

// Apply your specific code here.

}

// Do whatever you want

return $shortcode_feature;

}

Snippet -2 (Using strpos() function)

$shortcode = '[shortcode_name';

$check = strpos( $post->content,$shortcode );

if ( $check !== false ) {
//Return already matched

}

Prefer to use Snippet-1 using default function of wordpress.

Cheers

Comments

Popular posts from this blog

How to Get Access of Database of Dynamic Site Without cPanel Details

Create New Project in Laravel

How to Create Secondary Menu in Squarespace