Advanced Functions in WordPress

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 '<p>Hello from add_action!</p>';
}
add_action('wp_footer', 'my_custom_function'); // Hook into footer

2. add_filter()

Modifies existing data before it’s displayed.

function modify_title($title) {
return ' ' . $title;
}
add_filter('the_title', 'modify_title');

3. add_shortcode()

Registers a custom shortcode for use in posts/pages.

function my_shortcode_function($atts) {
return "Welcome to my site!";
}
add_shortcode('welcome', 'my_shortcode_function');

Use in editor:

[welcome]

4. do_shortcode()

Executes a shortcode from PHP code.

echo do_shortcode('[welcome]');

5. register_nav_menu()

Registers a single menu location.

function register_my_menu() {
register_nav_menu('main-menu', 'Main Navigation');
}
add_action('init', 'register_my_menu');

To display it:

phpCopyEdit<?php wp_nav_menu(['theme_location' => 'main-menu']); ?>

Custom Post Types (CPT)

register_post_type()

Registers a new content type.

function create_books_cpt() {
register_post_type('book', [
'labels' => [
'name' => 'Books',
'singular_name' => 'Book',
],
'public' => true,
'has_archive' => true,
'rewrite' => ['slug' => 'books'],
'supports' => ['title', 'editor', 'thumbnail'],
'show_in_rest' => true, // For Gutenberg
]);
}
add_action('init', 'create_books_cpt');

Access via: yourdomain.com/books/


Custom Taxonomy

register_taxonomy()

Registers a custom category or tag-like taxonomy.

function create_book_genre_taxonomy() {
register_taxonomy('genre', 'book', [
'labels' => [
'name' => 'Genres',
'singular_name' => 'Genre',
],
'hierarchical' => true,
'public' => true,
'rewrite' => ['slug' => 'genre'],
'show_in_rest' => true,
]);
}
add_action('init', 'create_book_genre_taxonomy');

Display Custom Post Types & Taxonomy

Loop for CPT:

$args = ['post_type' => 'book'];
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post();
the_title('<h2>', '</h2>');
the_content();
endwhile;
wp_reset_postdata();

Display taxonomy:

$genres = get_the_term_list(get_the_ID(), 'genre', '', ', ');
echo 'Genres: ' . $genres;

Widget Areas (Sidebars)

register_sidebar()

Registers a new widget-ready area.

function custom_widget_area() {
register_sidebar([
'name' => 'Sidebar Widgets',
'id' => 'sidebar-1',
'before_widget' => '<section class="widget">',
'after_widget' => '</section>',
'before_title' => '<h2>',
'after_title' => '</h2>',
]);
}
add_action('widgets_init', 'custom_widget_area');

dynamic_sidebar()

Displays a registered widget area:

<?php if (is_active_sidebar('sidebar-1')) : ?>
<?php dynamic_sidebar('sidebar-1'); ?>
<?php endif; ?>

✅ Summary Cheat Sheet

FeatureFunction
Hook into pageadd_action(), add_filter()
Shortcodesadd_shortcode(), do_shortcode()
Custom Menuregister_nav_menu()
CPTregister_post_type()
Custom Taxonomyregister_taxonomy()
Widget Arearegister_sidebar() + dynamic_sidebar()

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *