1

When I create pages within Wordpress, the call to action links on those pages always have the same URL. I want to be able to globalize this variable but am struggling to find a good way to do it.

If I were making simple PHP pages I could simply include e.g.

$URLpath = "http://example.com/my/page";

Then call this in the html:

<a href="<?=$URLpath?>">Call to action</a>

Obviously I can't do this within the Wordpress CMS as it renders as <?=$URLpath?>

I could add, say, <a href="#MyVariable#">Call to action</a> and replace after the page loads using javascript replace over the whole block of html, but it doesn't feel efficient to do this.

Is there a better way? I'm pretty new to Wordpress.

2
  • why couldn't you include a php script in wordpress? Commented Oct 16, 2015 at 8:51
  • Are you trying to add php code through wordpress visual editor? Commented Oct 16, 2015 at 9:29

3 Answers 3

1

You can add a shortcode in your functions.php file that prints your cta, or better create a plugin with some parameter that manages this link.

For example in functions.php you could add

function cta_func( $atts ){
   $URLpath = "http://example.com/my/page";       
   return '<a href="'.$URLpath.'">Call to action</a>';
}
add_shortcode( 'cta', 'cta_func' );

inside your pages/post you can write simply this shortcode.

...
[cta]
...

Further infos about shortcodes are in the reference of codex https://codex.wordpress.org/Shortcode_API

Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't technically answer my question but did give me an alternative way of thinking, so awarded the answer - thanks!
1

A good way to do this within WordPress is to use their wp_localize_script()

In your functions.php file you could use as:

// For sake of example I will put your data inside an array,
// but you can set a single value.
$dataForJS = array(
  $URLPath => "http://example.com/my/page"
);

// wp_localize_script() takes 3 parameters:
// 1. Name of the registered script, this is the name you used in wp_register_script()
//    here I am naming it "main" but it can be anything.
// 2. Name of the variable.
//    This name is then available in your JS and will contain your data.
// 3. The data you want to pass, it can be a single value or array
wp_localize_script( 'main', 'd', $dataForJS );

In your JavaScript, you will be able to access this data by just calling the variable d.

What this does is that if the page requests the "main" script, WordPress will add a script tag with your data inside it and makes sure it is placed before your "main" script runs.

Comments

0

wp_add_inline_script() was introduced in WordPress Version 4.5, and is now the best practice for adding a JavaScript variable to a page.

wp_add_inline_script( 'mainjs', sprintf("const myDomain = '%s'", MY_DOMAIN), 'before' );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.