• Resolved geertvanderheide

    (@geertvanderheide)


    I’m planning on using the add_option function to set some basic site-wide information. These are easy to retrieve and output in a theme file, but I’m wondering if there’s a way to add them to a page’s content through the editor in WordPress. I’d like to be able to display, for example, a phone number on several pages, which I’d then have stored as an option (in wp_options). This way, I could keep these basic pieces of information editable in one place, with references to it where needed.

    Example of what I’m hoping to achieve:

    (in the visual editor)
    “To get in touch, please call [option:phone_number] during business hours.”

    Does WordPress natively support anything like this, and if not, how would I go about achieving this?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Try it with the shortcode API:
    https://codex.www.ads-software.com/Shortcode_API

    Here’s an example:

    // shortcode [phone_number] in editor
    function phone_number_shortcode( $atts ) {
    
    	$number = get_option( 'phone_number' );
    
    	if ( !empty( $number ) ) {
    		return $number;
    	}
    
    	return '';
    }
    add_shortcode( 'phone_number', 'phone_number_shortcode' );

    Use [phone_number] in your content to display the phone number.

    Consider creating a child theme instead of editing your theme’s functions.php directly – if you upgrade the theme all your modifications will be lost. Or create a plugin with the code above by using a plugin or with the plugin documentation.

    Thread Starter geertvanderheide

    (@geertvanderheide)

    Ah of course, I did not think of that. Thank you very much for pointing me to a solution and including an example. Adding one or more shortcodes should work well for me here. Solved!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Display option value in editor (TinyMCE)’ is closed to new replies.