{"id":250,"date":"2025-07-27T06:47:55","date_gmt":"2025-07-27T06:47:55","guid":{"rendered":"https:\/\/mitalgoswami.in\/?p=250"},"modified":"2025-08-27T06:33:40","modified_gmt":"2025-08-27T06:33:40","slug":"advanced-functions-in-wordpress","status":"publish","type":"post","link":"https:\/\/mitalgoswami.in\/?p=250","title":{"rendered":"Advanced Functions in WordPress"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">1. <code>add_action()<\/code><\/h3>\n\n\n\n<p>Adds a function to a <strong>WordPress action hook<\/strong> (e.g., run code at a specific point like page load or admin init).<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>function my_custom_function() {<br>  echo '&lt;p>Hello from add_action!&lt;\/p>';<br>}<br>add_action('wp_footer', 'my_custom_function'); \/\/ Hook into footer<br><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"> 2. <code>add_filter()<\/code><\/h3>\n\n\n\n<p>Modifies existing data before it\u2019s displayed.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>function modify_title($title) {<br>  return ' ' . $title;<br>}<br>add_filter('the_title', 'modify_title');<br><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"> 3. <code>add_shortcode()<\/code><\/h3>\n\n\n\n<p>Registers a custom shortcode for use in posts\/pages.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>function my_shortcode_function($atts) {<br>  return \"Welcome to my site!\";<br>}<br>add_shortcode('welcome', 'my_shortcode_function');<br><\/code><\/pre>\n\n\n\n<p>Use in editor:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>[welcome]<br><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"> 4. <code>do_shortcode()<\/code><\/h3>\n\n\n\n<p>Executes a shortcode from PHP code.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>echo do_shortcode('[welcome]');<br><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"> 5. <code>register_nav_menu()<\/code><\/h3>\n\n\n\n<p>Registers a single menu location.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>function register_my_menu() {<br>  register_nav_menu('main-menu', 'Main Navigation');<br>}<br>add_action('init', 'register_my_menu');<br><\/code><\/pre>\n\n\n\n<p>To display it:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">phpCopyEdit<code>&lt;?php wp_nav_menu(['theme_location' =&gt; 'main-menu']); ?&gt;\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"> Custom Post Types (CPT)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"> <code>register_post_type()<\/code><\/h3>\n\n\n\n<p>Registers a new content type.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>function create_books_cpt() {<br>  register_post_type('book', [<br>    'labels' => [<br>      'name' => 'Books',<br>      'singular_name' => 'Book',<br>    ],<br>    'public' => true,<br>    'has_archive' => true,<br>    'rewrite' => ['slug' => 'books'],<br>    'supports' => ['title', 'editor', 'thumbnail'],<br>    'show_in_rest' => true, \/\/ For Gutenberg<br>  ]);<br>}<br>add_action('init', 'create_books_cpt');<br><\/code><\/pre>\n\n\n\n<p> Access via: <strong><code>yourdomain.com\/books\/<\/code><\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"> Custom Taxonomy<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"> <code>register_taxonomy()<\/code><\/h3>\n\n\n\n<p>Registers a custom category or tag-like taxonomy.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>function create_book_genre_taxonomy() {<br>  register_taxonomy('genre', 'book', [<br>    'labels' => [<br>      'name' => 'Genres',<br>      'singular_name' => 'Genre',<br>    ],<br>    'hierarchical' => true,<br>    'public' => true,<br>    'rewrite' => ['slug' => 'genre'],<br>    'show_in_rest' => true,<br>  ]);<br>}<br>add_action('init', 'create_book_genre_taxonomy');<br><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"> Display Custom Post Types &amp; Taxonomy<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Loop for CPT:<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>$args = ['post_type' => 'book'];<br>$loop = new WP_Query($args);<br>while ($loop->have_posts()) : $loop->the_post();<br>  the_title('&lt;h2>', '&lt;\/h2>');<br>  the_content();<br>endwhile;<br>wp_reset_postdata();<br><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Display taxonomy:<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>$genres = get_the_term_list(get_the_ID(), 'genre', '', ', ');<br>echo 'Genres: ' . $genres;<br><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"> Widget Areas (Sidebars)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"> <code>register_sidebar()<\/code><\/h3>\n\n\n\n<p>Registers a new widget-ready area.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>function custom_widget_area() {<br>  register_sidebar([<br>    'name' => 'Sidebar Widgets',<br>    'id' => 'sidebar-1',<br>    'before_widget' => '&lt;section class=\"widget\">',<br>    'after_widget' => '&lt;\/section>',<br>    'before_title' => '&lt;h2>',<br>    'after_title' => '&lt;\/h2>',<br>  ]);<br>}<br>add_action('widgets_init', 'custom_widget_area');<br><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"> <code>dynamic_sidebar()<\/code><\/h3>\n\n\n\n<p>Displays a registered widget area:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&lt;?php if (is_active_sidebar('sidebar-1')) : ?><br>  &lt;?php dynamic_sidebar('sidebar-1'); ?><br>&lt;?php endif; ?><br><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2705 Summary Cheat Sheet<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Feature<\/th><th>Function<\/th><\/tr><\/thead><tbody><tr><td>Hook into page<\/td><td><code>add_action()<\/code>, <code>add_filter()<\/code><\/td><\/tr><tr><td>Shortcodes<\/td><td><code>add_shortcode()<\/code>, <code>do_shortcode()<\/code><\/td><\/tr><tr><td>Custom Menu<\/td><td><code>register_nav_menu()<\/code><\/td><\/tr><tr><td>CPT<\/td><td><code>register_post_type()<\/code><\/td><\/tr><tr><td>Custom Taxonomy<\/td><td><code>register_taxonomy()<\/code><\/td><\/tr><tr><td>Widget Area<\/td><td><code>register_sidebar()<\/code> + <code>dynamic_sidebar()<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>1. add_action() Adds a function to a WordPress action hook (e.g., run code at a specific point like page load or admin init). function my_custom_function() { echo &#8216;&lt;p>Hello from add_action!&lt;\/p>&#8217;;}add_action(&#8216;wp_footer&#8217;, &#8216;my_custom_function&#8217;); \/\/ Hook into footer 2. add_filter() Modifies existing data before it\u2019s displayed. function modify_title($title) { return &#8216; &#8216; . $title;}add_filter(&#8216;the_title&#8217;, &#8216;modify_title&#8217;); 3. add_shortcode() Registers [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,2],"tags":[],"class_list":["post-250","post","type-post","status-publish","format-standard","hentry","category-unit-5","category-wordpress"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts\/250","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=250"}],"version-history":[{"count":1,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts\/250\/revisions"}],"predecessor-version":[{"id":251,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts\/250\/revisions\/251"}],"wp:attachment":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}