what is the difference between get_header_image() and header_image() in wordpress - wordpress

in the process of self learning from the official wordpress theme developement handbook
this page shows how to create headers and two different functions were used:
get_header_image()
header_image()
both return a string, which is the image URL.
is this just another confusing redundency ? or is there an actual difference between the two.

Semantic
dis·play
Make a prominent exhibition of (something) in a place where it can be easily seen.
the palace used to display a series of Flemish tapestries.
re·trieve
get or bring (something) back; regain possession of.
I was sent to retrieve the balls from his garden
How
While header_image() will echo out the header image URL, get_header_image() will not.
<?php
header_image();
get_header_image();
header_image() is a wrapper for get_header_image(). Mainly used on the front-end, the role is to escape and echo out get_header_image().
<?php
/**
* Displays header image URL.
*
* #link https://developer.wordpress.org/reference/functions/header_image/
*/
function header_image() {
$image = get_header_image();
if ( $image ) {
echo esc_url( $image );
}
}
A practical use case of get_header_image() would be inside a function.
WordPress use that get_ ... distinction for most of it's default functions, eg:
get_the_title() and the_title().
get_the_post_thumbnail() and the_post_thumbnail().
get_the_content() and the_content().
... etc.

I found the answer.
Someone posted it then deleted it quickly, but just for the sake of benefiting everyone who is looking for an answer:
header_image() will echo the URL without the need of using the php echo. While get_header_image() will also return the image URL but it doesn't echo it. You have to use the php echo for it.
NB: this isn't mentioned in the official documentation, unless am blind I challenge anyone to show me where it says echo in the official page, or where this distinction is explained.

Related

Is there a concise Wordpress function for building a page link from an ID?

I'm currently building links like this:
<?php echo get_the_title(111); ?>
I was building links like this using the WPML plugin (but steering away from it due to various reasons):
<?php icl_link_to_element(111); ?>
This builds a link similar to my first example.
So my question is is there a native Wordpress function that does this? I'm sure there must be, but cannot find the solution anywhere. I'm looking to reduce my markup...
Thanks!
EDITED WITH ANSWER
This is how I built my custom function:
function build_pretty_link($id,$link_title='') {
if($link_title=='') {
$link_title = get_the_title($id);
}
$link_url = get_permalink($id);
echo "{$link_title}";
}
WordPress give a function that print an anchor tag with the title and the url, but you have to be in a the loop (http://codex.wordpress.org/Function_Reference/permalink_anchor).
I suggest you to create your own function (the functions.php file in your theme is here for that).
You can do someting like that :
function vp_link_to($post_id) {
echo '<?php echo get_the_title($post_id); ?>';
}
get_permalink(x);
Where the ID of the page is x and wrap this in whatever you need, so
$id = 10;
$link = get_permalink($id);
echo 'Linked text';

hatom-entry errors on the Google snippet test

Almost sure that I'm not the first one that has this question but when I test my (WordPress) page on Google Snippet Test Tool I got hatom-enty errors like:
hatom-feed
hatom-entry:
Fout: At least one field must be set for HatomEntry.
Fout: Missing required field "entry-title".
Fout: Missing required field "updated".
Fout: Missing required hCard "author".
I found some tutorials about this but that is for standard WordPress themes. I'm using Inovado from ThemeForest and I can't figure out in which file I have to edit this data.
Someone familiar with this?
I also got problems with snippet review... Good in testresults but doesn't show up in de Google Search Results. Don't know why...
You can Add this code to the function.php file in your theme's directory and it will solve the problems.
//mod content
function hatom_mod_post_content ($content) {
if ( in_the_loop() && !is_page() ) {
$content = '<span class="entry-content">'.$content.'</span>';
}
return $content;
}
add_filter( 'the_content', 'hatom_mod_post_content');
//add hatom data
function add_mod_hatom_data($content) {
$t = get_the_modified_time('F jS, Y');
$author = get_the_author();
$title = get_the_title();
if(is_single()) {
$content .= '<div class="hatom-extra"><span class="entry-title">'.$title.'</span> was last modified: <span class="updated"> '.$t.'</span> by <span class="author vcard"><span class="fn">'.$author.'</span></span></div>';
}
return $content;
}
add_filter('the_content', 'add_mod_hatom_data');
The code contains 2 functions. The first function will use the WordPress filter hook for “the_content” to add the class of “entry-content” to the article. To add in the other essential hAtom fields, the second function will add a short sentence to the end of your post article, which contains updated time, post title and author, with required microdata.

How do I make my links in Wordpress not add itself to the existing URL?

I'm guessing there is a simple solution to this, but I can't seem to get my phrasing right when searching for it, so I'll post it here.
I have some links that look like this in Wordpress:
<a target="_blank" href="<?php echo get_post_meta($post->ID, $prefix.'hjemmeside', true); ?>"><?php echo get_post_meta($post->ID, $prefix.'hjemmeside', true); ?></a>
Just regular links that I echo in my single template to create user homepage/facebook etc. The problem is when you click it, the link will just adds itself to the end of the URL:
Example:
wordpress.com/single
when clicking the link:
wordpress.com/single/www.homepagelink.com
Thanks for any help :)
My guess is that wordpress isn't adding anything. If you have the URL without preceding http:// in the custom field it is shown that way by the browser. If you check the generated source code with your browser you will find the code like this:
<a target="_blank" href="www.homepagelink.com">www.homepagelink.com</a>
Without http:// or other valid URL schema this is interpreted by the browser as a relative link and handled as such.
You can either add the http:// in the field value or you place a wrapper function in the functions.php of your theme to make sure it is always interpreted as URL regardless what was put in the field.
function my_field_link($id, $field) {
$value = get_post_meta($id, $field, true);
if (substr($value, 0, 7) == "http://") return $value;
return "http://" . $value;
}
Then you can call this function like this:
<a target="_blank" href="<?php echo my_field_link($post->ID, prefix.'hjemmeside'); ?>"><?php echo my_field_link($post->ID, prefix.'hjemmeside'); ?></a>
Now the link will always start with http://.
Note: If you expect to have other URL schemas in use (https, ftp, scp, etc.) you should adapt the function accordingly.

Way to get Twitter button, with count, with custom bit.ly URL, working?

I'm stuck. I posted this on WordPress.StackExchange and they suggested I try at WebApps.StackExchange, and they suggested I try here. So, apologies for the multiple posts if you follow all those!
I have a client blog using bit.ly pro to generate custom short urls (ie foo.co). I want to show the regular horizontal version of the Twitter button, with tweet-count, and have the link that goes to the post use their custom bit.ly pro url.
I have installed Joost de Valk's Bit.ly Shortlinks plugin, which successfully converts normal WP shortlinks (wp_get_shortlink()) to the custom Bit.ly pro URL elsewhere in the site, but Twitter seems to trump that and render everything with the default t.co domain instead.
I've looked at the suggestions from this question but using the # as the data-url doesn't work, and the suggested Twitter support pages don't seem to contain any info on how to get Bit.ly to work (though they say they're going to).
Here's the function I created to insert the button in my theme - any ideas on where I'm going wrong? this is used to insert the button both within the Loop and on single-post pages.
function tweet_this() {
global $post;
ob_start();
$tweet = wp_get_shortlink();
echo '<script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script>Tweet';
return ob_get_clean();
}
In case it helps, this function does work, except it doesn't render the tweet-count:
function tweet_this() {
global $post;
ob_start();
$tweet = sprintf( __('%1$s %2$s'), $post->post_title, wp_get_shortlink() );
echo '<a class="tweethis" href="http://twitter.com/intent/tweet?text=' . urlencode( $tweet ) . ' via #clientname">Tweet this</a>';
return ob_get_clean();
}
Let me know if you need more info - and thanks in advance for any help you can throw my way!
Michelle
function tweet_this() {
global $post;
$tweet = get_permalink(); //replace with your code
$tweetmarkup = '<script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script>Tweet';
echo $tweetmarkup;
}
This works for me, but I don't have the WPShortlinks installed, so I replaced it with the permalink. You should be able to replace the permalink with your wp_get_shortlink and it should work.

targeting title in wordpress post

I am working on a wordpress plugin that modifies the title of a post. I only want to do this when I am viewing a single post. To be specific, I want to add a link beside the title, but for purposes of the question, I will be adding some arbitary text.
I started out by using the 'the_title' filter hook, and calling this function.
function add_button_to_title($title)
{
global $post;
if(is_single())
{
return $title.'googly googly';
}
return $title;
}
The problem is, the links on the side bar apparently also use 'the_title', as I saw my text showing up in the side bars as well, which led me to:
if(is_single() && in_the_loop())
But then, in my theme(and i suppose themes in general) there is a link to the previous post and next post, which also uses 'the title' filter. So finally I have:
if(is_single() && in_the_loop() && ($post->post_title == $title))
The last conditional basically makes sure that it is the title of the post that is being printed, not the title of the next or previous post. This works but I am not sure how well it will work given different themes...It seems like terribly hacked together. Any advice from wordpress gurus out there? I am worried that the title would be modified for other reasons and the conditional will fail.
Any help is appreciated!
Ying,
There isn't really a good solution except, as ShaderOp said, requiring theme modification. Your solution will work for the most part. The only exception is if the theme developer has changed the query in a page. I'd say this is probably a good enough solution that it'll cover more than 95% of the cases you'd run into.
I solved a similar issue by adding a check to see if the title being filtered matches the title of the post. This avoids the issue with other post titles on the page (in sidebar, menu) also getting filtered.
function add_button_to_title( $title ) {
global $post;
if( is_single() && $title == $post->post_title ) {
return $title . 'googly googly';
} else {
return $title;
}
}
Wouldn't it be easier to keep the original version of your add_button_to_title function, but instead of hooking it to a filter, call it directly from your single.php page in the appropriate place?
For example, somewhere in your theme's single.php, instead of this:
<h3 class="storytitle">
<?php the_title(); ?>
</h3>
Use this:
<h3 class="storytitle">
<a href="<?php the_permalink() ?>" rel="bookmark">
<?php echo add_button_to_title(the_title('', '', false); ?>
</a>
</h3>
today I ran into a similar problem. the_title gets called several times accross the whole page (e.g., in the html-head, the menus, the sidebar). I followed a similar approach using conditionals and the post/page id.
Additionally, I added a boolean flag which is set to true using the 'the_content' filter. So the title gets changed until the content is displayed. This way, I ensure that sidebars/widgets are not affected (e.g. Thematic theme has a default widget with links to pages - here the other conditionals would not be helpful as get_the_id() would return an equivalent). This ONLY works if the theme uses sidebars on the right. I did not find a way yet how to hook in directly before the 'the_title' call for the page/post to enable the boolean flag.
function myplugin_adjust_title($title, $id) {
global $myplugin_title_changed;
if ($myplugin_title_changed) {
return $title;
}
if (in_the_loop() && is_page('myplugin') && $id == get_the_ID()) {
$title = '';
}
return $title;
}
add_filter('the_title', 'myplugin_adjust_title', 10, 2);
function myplugin_adjust_title_helper_content($content) {
global $myplugin_title_changed;
$myplugin_title_changed = true;
return $content;
}
add_filter('the_content', 'myplugin_adjust_title_helper_content');

Resources