Wordpress: PHP Function inside a Shortcode. How? - php

Here's what I'd like to do. This function which grabs the content of a custom Wordpress post field:
get_post_meta($post->ID, 'society_twitter', true);
Needs to run in place of the string 'yourTwitterUsername' below,
[twitter-widget username="yourTwitterUsername"]
Looking something like:
[twitter-widget username="get_post_meta($post->ID, 'society_twitter', true);"]
Is this possible? If so, what is the syntax? A little more detail: I'm trying to modify a Twitter widget to display the username listed in a custom post field in my database rather than just one string value. I've tried editing the plugin php itself but this seems like an easier solution.

Use string concatenation?
$string = '[twitter-widget username="' . get_post_meta($post->ID, 'society_twitter', true) . '"]';
Edit: Updated based on the comments:
$string = '[twitter-widget username="' . get_post_meta($post->ID, 'society_twitter', true) . '"]';
echo do_shortcode( $string);

Related

Gettext doesnt work what I'm doing wrong?

I'm making small plugin to share the post in my word-press
Everything work but I can't get a string to be translatable throw wpml.
Bear with me it's not a question about wpml,
My problem is that I can't figure how to add Get text to my code in the right way.
Here is my code:
function add_social_share_icons($content)
{
$html = "<div class='social-share-wrapper'><div class='share-on'>Share on: </div>";
global $post;
$url = get_permalink($post->ID);
$url = esc_url($url);
if(get_option("social-share-facebook") == 1)
{
$html = $html . "<div class='facebook'><a target='_blank' href='http://www.facebook.com/sharer.php?u=" . $url . "'>Facebook</a></div>";
}
I want to translate this "Share on:" the wpml use the Gettext to detect the string.
Like this <th><?php echo __('Due date:', 'themedomain'); ?></th>
I tried something like this:
$html = "<div class='social-share-wrapper'><div class='share-on'><?php _e('Share on:'); ?></div>";
But it doesn't work!
Can someone help me please?
You can't embed a function like that into a string, only simple things can be done that way. Instead, you want to concatenate which can be done in a couple of ways, but this is probably the easiest. Also, instead of _e() which echos the text, I'm using __() to just translate
$html = "<div class='social-share-wrapper'><div class='share-on'>" . __('Share on:') . "</div>";
The second parameter of __() is the text domain, same as _e()

Change Wordpress %title variable output in previous_post_link

I'm using this code in our WordPress theme template to show the previous post which appears to work: previous_post_link('%link', '%title', true);
However, the content of %title includes the entire title of each post, which is very long and messes up the theme formatting.
Is there a way to modify the output of %title to limit to 20 characters?
Try this:
function modify_title_previous_post_link($link) {
preg_match('~>\K[^<>]*(?=<)~', $link, $match);
$title=$match[0];
$title = substr($title,0,20);
$title.="...";
$link=preg_replace('/<a(.+?)>.+?<\/a>/i',"<a$1>$title</a>",$link);
return $link;
}
add_filter('previous_post_link','modify_title_previous_post_link');
I'm not that good with regex, so I used this answer for preg_match and this answer for preg_replace.
I'm not a fan of the preg_match method.
Is there a reason that you have to pass '%title' into previous_post_link? The Wordpress function just does a text replacement on it, it's not really a complex macro or anything.
I'd do something more like
$prev_title = $get_previous_post()->post_title;
if (strlen($prev_title) > 20)
$prev_title = substr($prev_title , 0, 20) . '...';
previous_post_link('%link', $prev_title, true);
(I'm new to PHP so I might have gotten some syntax wrong, but that's the basic idea)

Wordpress: possible to use blog title when there is no search term?

In Wordpress I currently get the search time like so:
<?php $kw = str_replace("-"," ", $_GET['s']);
however, I would like it so that if there is no search term, the site title is used for $kw instead.
How best to achieve this? I have played around with get_bloginfo(); but no success.
Thank you for any help.
You can apply a filter to the get_search_query function and intercept the incoming query. if the query is empty use the site title else proceed as planned. see the code below (to be added to your functions.php file in your theme)
<?php
function my_search_query($query) {
$blog_title = get_bloginfo();
$query = (empty($query) ? $blog_title : str_replace("-", " ",$query);
return $query;
}
apply_filters( "get_search_query", "my_search_query" );
?>

WordPress: Hide page element if there is no database data to fill it

Hopefully this is simple to solve. I have a few elements which pull in form data previously entered into the database, which are added to the post as custom fields. I extract this using a php query as such:
<a href="<?php echo get_post_meta($post->ID, 'societywebsite', true); ?>" target="_blank" >Website</a>
Obviously, this appears in the Loop (content.php) and will get added to any appropriate post. Sometimes, however, there won't be any data because a user chose not to enter a website into the 'societywebsite' field on a form. When this happens, there's no need for a link to 'Website' to be there, because it wouldn't go anywhere useful.
What I'd like to know is how to have an If clause that checks if the data exists and then shows the link only if it does. I don't mind javascript, but the cleaner and less 'hacky' the solution, the better, as content.php will run multiple times for search results.
Thanks so much for any help or advice you can provide.
Another way to do this, more according to the Wordpress Codex is the following...
$societywebsite = get_post_meta($post->ID, 'societywebsite', true);
if ($societywebsite != '') {
echo '<a href="' . $societywebsite . '" target="_blank" >Website</a>';
}
You can add an 'else' at the end of this for debugging purposes.
Assuming that field is a string for a url, you could do something like this.
if (strlen(get_post_meta($post->id, 'societywebsite', true)) < 1) {
echo '<a href="' . get_post_meta($post->ID, 'societywebsite', true) . '" target="_blank" >Website</a>';
}
That will very simply check if that field has a string that is more than 1 characters. If it is more than 1 characters, it will display the link. If not...it won't do anything.

Why wont all of this string print?

I am just trying to generate a path with something like :
$PhotoName = the_title();
$DestinationFile = 'temp/watermarked/';
$DestinationFile .= $PhotoName;
$DestinationFile .= '.jpg';
the_title(); is a Wordpress function that gets the title of the post. If I echo just $PhotoName I see the name of the post as suspected. However if I echo $DestinationFile it will never print that part of the string so I would see something like temp/watermarked/.jpg, it never prints $PhotoName as part of it.
You need to pass false as the third parameter to get it to return the page title to you.
$PhotoName = the_title('', '', false);
See the WordPress reference for more details.
the_title() is a template tag that must be run within the loop. The title applies filters and prints the title to the screen.
Use get_the_title() to return the value in php.
When using a foreach loop with get_posts() use $post->post_title to get the value. If you want the filters applied: apply_filters( 'the_title', $post->post_title );

Categories