0

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!

0

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>';
    
1
  • Thanks Tolea, not exactly what I was thinking but that gives me an idea. Appreciate your help! – blogjunkie Jul 31 '13 at 0:14
0

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>
2
  • Thanks Morgan, will look up str_ireplace and preg_replace – blogjunkie Jul 31 '13 at 5:41
  • I made a working example today and updated my answer with the important parts of the code to get it working. Hope it helps! – Morgan Estes Jul 31 '13 at 19:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.