do_shortcode not working - php

I've been stuck on this for a while. I'm working on a wordpress site where I wrote the theme from scratch, I use php calls to get the wordpress functionality that I need in certain sections.
I'm trying to use a plugin, but calling it via
echo do_shortcode('[STORE-LOCATOR]');
just isnt working. Even when I switch to the default template and post that code, it still doesnt work. It simply echoes "[STORE-LOCATOR]"
Any help would be greatly appreciated.

[STORE-LOCATOR] is probably not a 'shortcode' in WordPress sense.
I encountered this on different plugin, Stream media player. They use the same syntax as shortcodes, but they are actually not.
Try using:
echo apply_filters( 'the_content',' [STORE-LOCATOR] ');
instead of do_shortcode, and see if it helps.

do_shortcode() returns a string.
I get it working by doing:
<?php echo do_shortcode(...); ?>

This is specific to the Store Locator plugin, not do_shortcode in general.
apply_filters can be an acceptable workaround for other plugins, but this does not work for Store Locator; you will only see an empty space and some controls. This is because it is looking for that shortcode in the page/post body to determine whether or not to include all of its js references at the top of the page. And without these references, nothing will work. See the sl_head_scripts function in sl-functions.php.
To change this behavior, simply modify that function to match based upon page title instead. In my instance I wanted it only on a "shop" page, so I commented out the entire $on_sl_page test and replaced it with this:
$on_sl_page = ( strpos($pagename, 'shop') === 0 );
I then called it from my page with apply_filters as indicated in the other answer:
echo apply_filters( 'the_content','[STORE-LOCATOR]');
And this appears to work perfectly.

echo do_shortcode('[STORE-LOCATOR][/STORE-LOCATOR]');

Try using shortcode after the WordPress environment has been set up.
function my_function() {
echo do_shortcode('[STORE-LOCATOR]');
}
add_action('wp', 'my_function');

If you're writing the whole thing from scratch, you'll want to make sure that the function you create is in the root php file of your plugin. The function might look something like this, but you'll have to sub in whatever logic you're using to arrive at the store location:
<?php
function doCoolStuff () {
$var1 = "value1";
$var2 = "value2";
$output = $var1+$var2;
}
return $output;
}
add_shortcode('SOTRE-LOCATIOR', 'doCoolStuff');
?>
Then in your template put the code:
<?php echo do_shortcode('[STORE-LOCATOR]');?>
Happy coding and good luck!

Related

Changing woo commerce HTML that gets outputted with do_action

I'm trying to build a webshop with woo commerce which seems like a very nice and easy to use system. But I'm really having some trouble with it. A lot of times there's a function like this in the code: do_action( 'woocommerce_checkout_order_review' ) which outputs some HTML that I really want to change. The documentation that is provided isn't making me any wiser. How can I change this HTML. I would really appreciate any help trying to understand how this works!
The answer to the question, how do I modify the content ouputted in woocommerce_checkout_order_review ?
The answer:
The HTML is located in the template file here:
wp-content/plugins/woocommerce/templates/checkout/review-order.php
You can easily modify this content in a plugin or theme. Read more about that here: http://docs.woothemes.com/document/template-structure/
do_action( 'woocommerce_checkout_order_review' )
this is action declared somewhere, this is done so that whenever you want to add anything for example say "hello there", you dont have to hardcode it .. you can simply do it this way ..
function add_something() {
echo 'hello there';
}
add_action('woocommerce_checkout_order_review','add_something');
Refer here
Add Action
This would add "hello there" in the rendered html page..exactly where you have do_action('woocommerce_checkout_order_review'); in the code . keep this snippet in your functions.php and play with it ;)

Replacing Wordpress comment_form

I'm trying to replace the comment_form with custom built solution. But I cannot seem to figure out how to do this and the code is lacking:
http://codex.wordpress.org/Function_Reference/comment_form
Ideally, anytime that the comment_form function is called, I want to replace it with an iframe to another site. Is there a way of doing this?
there is no way you can overwrite comment_form() function,
if you want to overwrite comment_form then its not possible in right way.
instead you can you use filter like comment_form_before and comment_form_after to achieve what you want.
i am putting this answers because i just see code of comment_form()
something like
<?php add_action('comment_form_before',function(){
echo '<div style="display:none;">';
});
add_action('comment_form_after',function(){
echo '</div><iframe><iframe>';
});
?>

PHP within a Wordpress shortcode

I've got a PHP shortcode for Wordpress, let's call it [myshortcode].
The shortcode allows you to enter a download URL like this [myshortcode download="http://www.example.com/file.pdf"].
I want to use this shortcode in a template file, but have the download url be a variable. Is this possible, and if so, how do I do it?
I tried these, but it doesn't work...
<?php echo do_shortcode('[myshortcode download="<?php echo $variable['dllink']; ?>"]'); ?>
<?php echo do_shortcode('[myshortcode download="echo $variable['dllink'];"]'); ?>
Any ideas?
<?php echo do_shortcode('[myshortcode download="'.$variable['dllink'].'"]'); ?>
<?php echo do_shortcode("[myshortcode download=\"{$variable['dllink']}\"]"); ?>
I suggest you read this properly to understand why those work and yours didn't ;-)
I think it depends on where the variable is defined. For instance, if the shortcode is part of a plugin that allows the admin to set some options on a settings page, then you would want php to retrieve those options and echo the value into the shortcode.
Another way to do it would be to use a form with a $_POST method to submit a value to a table in the database, and then to call that value and set it to the variable.
In a recent plugin I wrote (Recent Post Views), I included a shortcode which retrieved options from the database and inserted those as variables:
//Get options from the settings page
$options = get_option('cb_rpv_options');
$select_bold = $options['select_bold'];
$select_italics = $options['select_italics'];
$select_list_style = $options['select_list_style'];
These variables changed the output of the shortcode, which is what you're trying to do. The best thing to do may be to download a plugin with well commented code as an example and build off that.
I hope this is helpful, and I agree with the other post that you need to clean up your syntax too.

WordPress Custom Short Code help

Anyone familiar with WordPress shortcodes? I could really use a hand! I've inserted the following code into my functions.php file for the theme I'm using...
function create_slideshow_header($atts, $content = null){
return '<div class="item_heading">'.$content.'</div>';
}
add_shortcode('slideshow_heading', 'create_slideshow_header');
function create_slideshow_white_header($atts, $content = null){
return '<span id="dyn">'.$content.'</span>';
}
add_shortcode('slideshow_heading_white', 'create_slideshow_white_header');
function create_slideshow_content($atts, $content = null){
return '<div class="item_content">'.$content.'</div>';
}
add_shortcode('slideshow_content', 'create_slideshow_content');
Now, I was led to believe by several tutorials that this should allow me to insert the following into the text editor in the WP backend...
[slideshow_heading]SLIDESHOW HEADER[/slideshow_heading]
...and the SLIDESHOW HEADER text would be wrapped in the appropriate HTML.... but it's just displaying the above as regular text. I've cleared my cache, etc...
Is there something I'm doing wrong? Thanks in advance!
SOLUTION
I failed to mention that I was using the page.ly MultiEdit plugin--which uses "custom fields" to create extra editable regions. WordPress conveniently doesn't parse shortcodes in custom fields. Normally, you can create a filter for each custom field, but since this is a plugin, you can just edit the multiedit.php file, and change line 63 from
echo $GLOBALS['multiEditDisplay'][$index][0];
to
echo apply_filters('the_content',$GLOBALS['multiEditDisplay'][$index][0]);
With a little work, you can turn Wordpress into a truly amazing CMS!
I actually checked out your code, it works for me. Your usage is right.
Try adding a die() in there to see whether the method is called in your case.

putting a php code into another php code! Possible?

i'm a wordpress user and i have a php code, and in that php code there is an area to put a url in: $url = "http://blabla.com"; well in wordpress you can call post permalinks with this code: <?php the_permalink(); ?> What i want to do is putting <?php the_permalink(); ?> instead of http://blabla.com above in the php code. Target is: getting permalinks put them there in php code, and let the php code use them to do its job. Is that possible? If yes, how with an example please...Thank you...
You could assign the return value of get_permalink() to $url:
<?php $url = get_permalink(); ?>
get_permalink() is different from the_permalink() because it doesn't display the link, but just returns it. (Originally this answer naively used the_permalink(), but I did some extra research to be sure.)
The above answer will not work. Use get_permalink().
the_permalink will display it's output
get_permalink will return the value.
<?php $url = get_permalink(); ?>
You need to understand the concept of some Template Tags, since the_permalink fits this category. They are defined especially for use in WordPress Themes. They can be summarize as "a code that instructs WordPress to "do" or "get" something".
Writing the_permalink simply echoes the permalink in your template. It's not something that you get in a php function and manipulates it. It just echoes the html of the information it needs to show.
This is useful for designers working out template files in Wordpress themes: they don't need to understand a lot of programming or a lot of php keywords: they just need to know that writing "the_permalink" gives them the desired html output.
What you're trying to do is get the output from a template tag that already outputs it's value. You need to use other template tag that RETURNS the value you want to use instead of ones that OUTPUTs it.
In your example, you need the get_permalink. Since the_permalink is used in the Loop, you need to provide a post id to your get_permalink function.
There are another examples that fits the same problem domain: for example, I can't manipulate what wp_list_pages returns (because it automatically outputs it's result), so I need get_pages (that RETURNS an array) to manipulate it's result.
Read Wordpress official documentation at Codex. It's great.

Categories