putting a php code into another php code! Possible? - php

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.

Related

Include PHP in Wordpress Shortcode

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

Wordpress template tag inside of a another template tag

Here's a fun one. I'm using advanced custom fields inside of a template to pull a field called Applications with the following code.
the_field('applications');
The problem is that this won't pull the actual content without the page ID along with it, for example:
the_field('applications','42');
where the page ID is 42.
What I would like to do is use another template tag to pull the existing page ID with something like this in place of the 42, so that it will content specific to that page:
the_ID();
Which, in a perfect world would look like so:
the_field('applications','the_ID();');
This is obviously ridiculous and doesn't work, but I don't know what I need to do to get this to actually work.
Thanks!
the_ID() will automatically echo whatever is returned, whereas get_the_ID() will return that value so that it can be stored in a variable, or passed as an argument (which is the deal in this case).
<?php the_field('applications',get_the_ID()); ?>
http://codex.wordpress.org/Function_Reference/get_the_ID

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.

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.

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!

Categories