Include PHP in Wordpress Shortcode - php

I'm trying to use this in a loop but I'm unsure how to implement it. Its a like2unlock plugin and I'm using it within a loop!
<?php echo do_shortcode('[l2g]<span class="download left"><?php get_attachment_icons($echo=true); ?></span>[/l2g]'); ?>
This code oviously wont work in my wordpress template, what are your thoughts for a resolution?

You just need to build your shortcode string:
echo do_shortcode('[l2g]<span class="download left">' . get_attachment_icons(true) . '</span>[/l2g]');
You don't need to "Include PHP" in your short code, you need to use PHP to build your shortcode.
Also, $echo = true is the notation for specifying a default parameter in a function definition. You should just pass the parameter without assigning it to a variable: get_attachment_icons(true). Since true is the default you can also call the function without specifying any parameter.
Incidentally, get_attachment_icons($echo=true) will work correctly since an assignment evaluates to the value being assigned, but it is still not the correct way to call a function with a default parameter.

You should not be passing php into shortcode attributes. It is really bad practice and also opens up a loop hole for hackers to exploid.
The correct way is to build your php executable code into your shortcode and then using the attributes to control how your php operates within the shortcode. get_attachment_icons should be inside your shortcode. How you want to control the output can be in your attributes and passed as such

Related

Wordpress PHP How to Get a Function's Return Value Into an If() Statemente

I'm trying to create and if() statement that works in accordance to the kind of post (i.e., page or article) and, if page, the title of such page.
The type of post can be obtained from its class function post_class() and the title from its title function the_title().
So I know where I can get the info I need but, then, no matter what, I cannot turn this info into a string I can test. Wherever I put any of these two functions I get an output onto the page.
First, I tried:
if(strpos(post_class(), 'page')) {
//DO SOMETHING
}
Didn't work. Just had the post_class() dumped onto the page.
Then, I tried calling the function as the value of a variable:
$this_class = post_class();
And had the same result.
I've since tried a couple of other dirtier ways of doing it but to no avail. It seems wherever these WP functions are placed, they will dump their values onto the page.
Perhaps somebody out there knows how I can successfully get the type of post and title and set them to if() statements in order to trigger whatever else.
Thanks!
According to Wordpress Docs:
post_class()
When the post_class function is added to a tag within the loop, for
example >, it will print out and add
various post-related classes to the div tag.
In case you would like to be able to retrieve the value you should use
the get_post_class() function which returns that value.
Retrieve the classes for the post div as an array.

How to alter/remove this function's output without modifying the original theme files?

I'm using a theme framework and am trying very hard to resist the temptation of editing core files. I want to add the functionality of post formats, but I need to be able to remove certain elements for specific post formats.
function thesis_teaser_headline($post_count, $post_image) {
thesis_hook_before_teaser_headline($post_count); #hook
if ($post_image['show'] && $post_image['y'] == 'before-headline')
echo $post_image['output'];
echo '<h2 class="entry-title">' . get_the_title() . "</h2>\n";
if ($post_image['show'] && $post_image['y'] == 'after-headline')
echo $post_image['output'];
thesis_hook_after_teaser_headline($post_count); #hook
}
What would be the most efficient way to go about removing headline data for a post format such as 'link' (for example)? This function is being called to generate the content for the teasers from the homepage loop. I could just make an entire custom loop, but it won't tie in with the Thesis backend which makes it much less flexible.
Thanks!
I'm not familiar with Thesis, but I see it is calling hooks, so you could possibly put one of your functions on these, that in case you don't want any content from original thesis_teaser_headline() would turn on output buffering (ob_start()) on before hook, and clean it (ob_end_clean() or $content = ob_get_clean() if you want to alter its content, not completely replace) on after hook.
You can use the /* */ control to make the function in comment. So it will not be executed. Or put just the section you want to delete between them...
What about tweaking the html with jquery? Check on the post classes (the list of css classes that's on the div that wraps each post) to see if you can select what you need from there.
On top of all, I recommend to avoid using that framework, it's pure nightmare when you need to tweak it.

How can I pass a variable wordpress shortcode

I'm writing a Wordpress plugin which dynamically creates a string.
I want to insert this string in a meta tag using the wp_head hook.
My plugin works as follows: I define a shortcode whose handler function declares an add_action(... that adds a special <meta> tag into the header.
This works, BUT...
My only problem is that I can't figure out how to pass the variable containing the string to get printed into the head. The variable isn't defined, even though I globalize it in my plugin.
//INSIDE MY PLUGIN...
global $head_string;
function initialize_my_plugin_class() {
$head_string = "<meta>bla bla bla</meta>";
}
function add_action('wp_head', 'add_meta_tags' ) //this is slightly oversimplified
//the execution order of wp_head makes
//the real code a bit more complicated
//for the purposes of this question let's
//pretend it's like this.
function add_meta_tags() {
echo $head_string
}
The result works, EXCEPT that the $head_string variable is empty. So it prints an empty meta tag. (I know everything else works because I can change add_meta_tags() to do something like echo "FAKE META TAG"; and it shows up where it should in the header.)
So what's wrong with what I'm doing? I think it must involve variable scope, but I'm stuck.
Try setting $head_string as global inside your functions.

do_shortcode not working

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!

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