Shortcode or placeholder for theme options page?

I'm writing a theme options page that provides a space for the user to enter code for social buttons e.g.

a href="https://twitter.com/share" class="twitter-share-button" 
    data-url="?php echo get_permalink(); ?"Tweet/a

Obviously, having ?php echo get_permalink(); ? for the data-url attribute is not going to work in a theme options textarea. What are my options here? I've seen others user

  1. shortcodes e.g. [permalink] - but how can I process the shortcode?

  2. placeholders e.g. {{url}} - again, how do I replace these placeholders?

Appreciate any pointers in the right direction. Thanks!

Topic theme-options shortcode Wordpress

Category Web


I would go with the placeholder method you mentioned.

You can provide the user with a list of "acceptable" placeholders that you then do a regex replacement on.

The trick is to store the string with the placeholder in the database so it displays back to the user the way they entered it next time they visit the options page. Do your str_ireplace or preg_replace magic on it before you use it elsewhere in the theme.


EDIT:

Here's the code I came up with today that will do this. I'm assuming you are already familiar with setting up your options page. This will let you safely save something like <a href="{{url}}">{{url}}</a> into the wp_options table and use it inside your theme as <a href="http://www.wp.dev/hello-world/">http://www.wp.dev/hello-world/</a>

Options page to save the HTML with a {{url}} placeholder:

public function parse_placeholder_url( ) {
    $placeholder_url = esc_html( $_POST['wpse108301']['placeholder_url'] );

    if ( isset( $placeholder_url ) && is_string( $placeholder_url ) ) {

        if ( get_option( 'wpse108301_placeholder_url' ) === false ) {
            add_option( 'wpse108301_placeholder_url', $placeholder_url );
        }
        else {
            update_option( 'wpse108301_placeholder_url', $placeholder_url );
        }
    }
    else {
        $placeholder_url = '';
    }

    return $placeholder_url;
}

public function create_a_url_field() {
        ?><input type="text" id="wpse108301_placeholder_url" name="wpse108301[placeholder_url]" value="<?php echo stripslashes( get_option( 'wpse108301_placeholder_url' ) ); ?>" /><?php
    }

functions.php in your theme:

function test_link() {
    $db_url = stripslashes( get_option( 'wpse108301_placeholder_url' ) );
    $url = preg_replace( '/\{\{\burl\b\}\}/i', get_permalink(), $db_url );

    return $url;
}

Inside your theme template pages:

<p><?php echo test_link(); ?></p>

I am using shortcodes. But you need to work differently with this and make it easier for the client.

  • On the theme options page you should have inputs with type='checkbox' where the client chooses what share buttons to appear.
  • In the template you should get the option with get_option('registered_option') and display those buttons for which the checkbox was checked in the theme options.

    if( get_option('tweeter_checkbox') )
      echo '<a href="https://twitter.com/share" class="twitter-share-button" 
        data-url="' . get_permalink(); . '">Tweet</a>';
    

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.