Using WordPress ‘after_setup_theme’ PHP action

The after_setup_theme WordPress action fires after the theme is loaded, allowing you to execute custom code at this point in the WordPress loading process.

Usage

add_action('after_setup_theme', 'your_custom_function');

function your_custom_function() {
    // your custom code here
}

Parameters

  • No parameters for this action.

More information

See WordPress Developer Resources: after_setup_theme

Examples

Register Custom Post Type

Register a custom post type ‘product’ after the theme is loaded.

add_action('after_setup_theme', 'register_custom_post_type_product');

function register_custom_post_type_product() {
    // Register custom post type 'product'
    register_post_type('product', array(
        'label' => 'Products',
        'public' => true,
    ));
}

Add Theme Support for Post Thumbnails

Enable post thumbnail support for your theme.

add_action('after_setup_theme', 'add_post_thumbnail_support');

function add_post_thumbnail_support() {
    // Enable post thumbnail support
    add_theme_support('post-thumbnails');
}

Register Navigation Menus

Register primary and secondary navigation menus for your theme.

add_action('after_setup_theme', 'register_theme_menus');

function register_theme_menus() {
    // Register primary and secondary navigation menus
    register_nav_menus(array(
        'primary' => 'Primary Menu',
        'secondary' => 'Secondary Menu',
    ));
}

Set Custom Image Sizes

Define custom image sizes for your theme.

add_action('after_setup_theme', 'set_custom_image_sizes');

function set_custom_image_sizes() {
    // Set custom image sizes
    add_image_size('custom-thumbnail', 300, 200, true);
    add_image_size('custom-medium', 600, 400, true);
    add_image_size('custom-large', 1200, 800, true);
}

Set Content Width

Define the content width for your theme.

add_action('after_setup_theme', 'set_theme_content_width');

function set_theme_content_width() {
    // Set content width
    global $content_width;
    $content_width = 800;
}