0

I'm trying to create some custom shortcode - I want to be able to have names and numbers of people I want to display on my website, but as a shortcode, so it doesn't have to be typed in again and again.

So for instance to have [joe] relate to 'Joe Bloggs - 01925 265646'

I'm currently using the Ultimate Tables plugin and would want to put the shortcode into this, and then for it to come up with the longer information.

I've been playing around for a few hours and still struggling - I've tried putting it directly into functions.php and have tried using the 'Shortcodes Pro' plugin but I can't figure out how to make it work (sorry, I'm not a massively advanced Wordpress user!).

This is the code I put into the functions.php section of the theme:

// Add Shortcode
function joe() {
}
add_shortcode( 'Joe Bloggs - 01925 265646', 'joe' );

Pretty much, my aim is to avoid having to manually enter phone numbers because on our old website this led to the phone numbers being entered incorrectly sometimes, as they are entered about dozens of times.

Thanks so much for your help in advance.

0

You are passing the parameters wrong, try with something like this:

function get_telephone($attrs) {
$attr = shortcode_atts( array(
      'name' => 'John Doe'
  ), $attrs );
return attr['name'];
}
add_shortcode( 'telephone', 'get_telephone' );

In the post you call the shortcode like this: [telephone name="Jose"]

Anyway, inside the get_telephone function you should add some logic to get the phone numbers from somewhere, it could be an array, or you could create a custom post type to store the numbers.

  • Thanks for your help! So do I put the code above into the functions.php section? – Connor Sep 16 '14 at 9:36
  • Also, sorry still a bit confused on how I can get phone numbers to link in - what logic would I use for this? – Connor Sep 16 '14 at 9:37
  • That's a design decision, you could hardcode it, which isn't the best option, or you could build a Custom Post Type to load the complete name, number and something else. Or create a custom table in the database. – Tomás Cot Sep 16 '14 at 11:47
-1

Connor, I had a similar problem with trying to display product prices as shortcodes. Here is what I did.
1. created a custom table in the wordpress database (called it wp-products)
2. this table had the following fields: id, make, model, price
3. Created a shortcode to retrieve the price. Shortcode looks like this [product_price id=2] where id=2 is the price value for item 2 in the table.
4. wrote the shortcode function for this and put it in functions.php in my themes directory.
5. added the shortcode into the page location where I wanted to display the code.

Here is the code, I hope it helps

function product_price_func( $atts ) {
    global $wpdb;

    $table_name = $wpdb->prefix . 'products';

    $atts = shortcode_atts(
    array(
      'id' => 'no id found',
    ), $atts, 'product_price' );

    $product_id = $atts['id'];

    $product_price = $wpdb->get_results("SELECT price FROM " . $table_name ." WHERE id={$product_id}", ARRAY_A);

    foreach ( $product_price as $the_price ) 
      { 
        $output = $the_price['price'];
      }

    return "Product Price: $" . $output;
}
add_shortcode( 'product_price', 'product_price_func' );

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.