Categories: Wordpress Development

Get Page ID by SHORTCODE in WordPress

For WordPress developer, sometimes we are required to know in which page the shortcode is placed and this example will show the function to get that page id/permalink within very small pieces of code.

Just paste the below code in functions.php file:

 

 

function wphats_get_page_url_by_shortcode($shortcode) {
 global $wpdb;

 $url = '';

 $sql = 'SELECT ID
  FROM ' . $wpdb->posts . '
  WHERE
   post_type = "page"
   AND post_status="publish"
   AND post_content LIKE "%' . $shortcode . '%"';

 if ($id = $wpdb->get_var($sql)) {
  $url = get_permalink($id);
 }

 return $url;
}

Now if you want to get the URL of your desired shortcode just call that function with SHORTCODE name. Example if your shortcode name is [CONTACT-FORM] then call the below function:

wphats_get_page_url_by_shortcode('[CONTACT-FORM]');

I hope this tutorial helps lots of WordPress developers to get a nice solution. If anything still you want to know, feel free to contact below.

Thanks.

This post has already been read 2763 times!

Mehedi Hasan

Cool WordPress Developer having much agile experience to develop any kind of WordPress sites & plugins. Also good in troubleshooting, fixing & making any kind of tweaks for WP site.

More Posts

Mehedi Hasan

Cool WordPress Developer having much agile experience to develop any kind of WordPress sites & plugins. Also good in troubleshooting, fixing & making any kind of tweaks for WP site.

Share
Published by
Mehedi Hasan
Tags: shortcode

Recent Posts

WP Share A Friend – A Way to Capture Lead

What is Lead Capture: In general sense lead capture designates a process or way of…

5 years ago

Unlimited Modal POPUPS on MouseClick

Introduction Unlimited Modal POPUPS on MouseClick: Everyone knows about popup during site visits and most…

5 years ago

WordPress Pregnancy Calculator Plugin

Brief about Pregnancy Calculator : This WordPress Pregnancy Calculator plugin allows users to display an…

5 years ago

Interactive Service Plugin with Hover Effects VC

Introduction About this Service Plugin: This Visual Composer Service Plugin works as an addon to…

6 years ago

WP Dynamic Query String

Introduction of Dynamic Query String : This plugin allows user to set dynamic query string…

6 years ago

How to make scroll PDF in iFrame for iPAD Safari

There is an occasional bug for iPhone and iPad even they are designed as well and…

7 years ago