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>