Search code examples
wordpresswordpress-themingwordpress-shortcode

Title shortcode how to work with new theme


I have a problem after change my theme for update the PHP to 8 version, there is a shortcode for the title like that:

[title size="H3" bold="no" align="left" color="#226dae" style="" border_color="" margin_top="" margin_bottom=""]...[/title]

[title size="H5" bold="no" align="left" color="#226dae" style="" border_color="" margin_top="" margin_bottom=""]...[/title]

What I have to do for have the right view for the title? Add a code in function.php?

I tried small code on function.php but seems not target my shortcode and the title don't show.


Solution

  • It seems like you were using a theme that provided shortcodes functionality for custom title display, which is not present in the new theme you have switched to. If you want to continue using the shortcode from the previous theme, you'll have to recreate this functionality in your new theme.

    You can do this by adding a shortcode function in your theme's functions.php file. Below is an example of how you can create a function that handles your specific title shortcode:

    function custom_title_shortcode($atts, $content = null) {
    extract(shortcode_atts(
        array(
            'size' => 'h2',
            'bold' => 'no',
            'align' => 'left',
            'color' => '#000000',
            'style' => '',
            'border_color' => '',
            'margin_top' => '',
            'margin_bottom' => ''
        ), $atts)
    );
    
    $style_attributes = 'text-align:'.$align.'; color:'.$color.'; border-color:'.$border_color.'; margin-top:'.$margin_top.'; margin-bottom:'.$margin_bottom.';';
    
    if ($bold == 'yes') {
        $style_attributes .= ' font-weight:bold;';
    } else {
        $style_attributes .= ' font-weight:normal;';
    }
    
    $style_attributes .= ' '.$style; // Include additional style
    
    return '<'.$size.'style="'.$style_attributes.'">'.$content.'</'.$size.'>'; 
    }
    
    add_shortcode('title', 'custom_title_shortcode'); `
    

    This function creates a shortcode [title] which accepts different parameters to customize the appearance of the title, such as size, alignment, color, border color, top margin, and bottom margin. The $content variable between the opening and closing shortcode tags will be used as the text for the title.

    After adding this to your functions.php file, you should be able to use your [title] shortcode in your posts and pages. The shortcode parameters can be set in the format [title parameter="value"]Your title text[/title].

    However, please note that directly editing a theme's functions.php file might not be the best practice as these changes will be lost when the theme gets updated. A better approach would be to create a child theme or to use a custom plugin to add your shortcodes.