1

I'm looking for a plugin that I can control globally on my Wordpress site. I want to create a custom list of words/content attach it to a shortcode. That shortcode is on every page I can then choose what word goes on what page easily and quickly rathger than have to edit every page individually. e.g. of global control (locations are page names and items mark with [] are shortcodes

enter image description here

3
  • Does each location match up to a page exactly? Jan 18, 2017 at 17:27
  • The ask in this question is unclear to me. What exactly do you want to be able to do, where? Select alternatives from a dropdown list? In the post editor?
    – CK MacLeod
    Jan 18, 2017 at 17:57
  • Do you mean you need to pass someting like id to select the array you want to display?
    – user111017
    Jan 19, 2017 at 6:07

1 Answer 1

2
+50

You should insert the following code in functions.php of your theme.

/* Global matrix for shortcodes/content for every page */
$locations = array(
    array(
        'location'  => 'location 1',
        'telephone' => '0121 34838383',
        'email'     => '[email protected]'
    ),
    array(
        'location'  => 'location 2',
        'telephone' => '92939393',
        'email'     => '[email protected]'
    ),
    array(
        'location'  => 'location 3',
        'telephone' => '343443433',
        'email'     =>  '[email protected]'
    ),
    array(
        'location'  => 'location 4',
        'telephone' => '343433',
        'email'     => '[email protected]'
    ),
    array(
        'location'  => 'Global Matrix Page',
        'telephone' => '222 33 22',
        'email'     => '[email protected]'
    )
);

function telephone_shortcode() {
    global $locations;
    $title = get_the_title();
    $key = array_search($title, array_column($locations, 'location'));
    if ($key)
        return $locations[$key]['telephone'];
    else
        return '';
}
add_shortcode('telephone', 'telephone_shortcode');

function email_shortcode() {
    global $locations;
    $title = get_the_title();
    $key = array_search($title, array_column($locations, 'location'));
    if ($key)
        return $locations[$key]['email'];
    else
        return '';
}
add_shortcode('email', 'email_shortcode');
/* End of Global Matrix */

$locations array contains your locations (page titles), telephones and emails. You can expand it as far as you need.

Below are two fucntions defining shortcodes on your pages: [telephone] and [email]. They are similar. Each fucntion get the title of current page (get_the_title) and looks for it in $locations array, then returns corresponding telephone or email.

Your page can be like that:

telephone = [telephone]

email = [email]

Code is tested, you can see result here.

5
  • Mine isn't returning a value only the else return ''
    – Tom Rudge
    Jan 20, 2017 at 17:52
  • What is your php version? Jan 20, 2017 at 20:29
  • 5.6.27 ? latest wordpress - same as yours. I tried adding it to my functions.php but it broke the site. I also tried adding it in as a seperate plugin... this is where i get the return ''; - so it sort of works but does not return the function only the else
    – Tom Rudge
    Jan 22, 2017 at 19:27
  • I solved this if($key !== false) but I also had to make column arrays work with php v5.3 - thanks for this.
    – Tom Rudge
    Jan 23, 2017 at 8:58
  • So, you have php 5.3 or 5.6? This code works from php 5.5 Jan 23, 2017 at 9:24

Your Answer

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

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