What is different between post_navigation() and get_the_post_navigation();? - wordpress

What is difference between these WordPress functions, and how to implement it?
the_post_navigation(); get_the_post_navigation();
the_archive_title(); get_the_archive_title();
the_post_pagination(); get_the_post_navigation();
the_archive_description(); get_the_archive_description();
I already googled for this, but I'm still not getting it right.

All the functions that starts with get_ are only returning the "result" of the function : If you put this function in a php page and watch this page in a browser, nothing will be displayed.
If you want the result to be displayed, you have to add echo before the function and this is exactly what the function that starts with the_ are doing.
You may ask yourself why sometimes we want only the result to be returned by the function and not displayed. It's because sometime, we have to do some additional operations with the result before displaying it.
Example:
$content = get_the_content();
$content = str_replace('Hello', 'Bye', $content);
echo $content;
If not operation are needed so you only need to do:
the_content();
You also ask "How to implemenent it ?". To implement the function, you will have to add it in some specific php files. For example, for the get_the_post_navigation() function you will have to add it in the single.php file in your theme folder. You will need basics on php.

Related

Woocommerce Display Category Image - get_woocommerce_term_meta deprecated

I'm in need of some help.
I want to display the category image on the current category page, and I have googled this, and each answer I find uses the same code.
They all use get_woocommerce_term_meta to retrieve the ID of the thumbnail used so that you can then use wp_get_attachment_url to get the image address.
All sounds great, but whenever I try this code it returns nothing, and I think that it is because get_woocommerce_term_meta is deprecated.
Does anyone know of a way round this, so I can get the image address when I have the category ID?
This is the code that I have in place:
global $wp_query;
$cat = $wp_query->get_queried_object();
$thumbnail_id = get_woocommerce_term_meta($cat->term_id, 'thumbnail_id', true);
echo $cat->term_id."<br />";
echo $thumbnail_id;
$image_src = wp_get_attachment_url($thumbnail_id);
$cat->term_id returns the correct ID of the category, but $thumbnail_id returns 0.
The code is in header.php.
get_woocommerce_term_meta() may be deprecated however it hasn't yet been removed. The issue is elsewhere in your setup.
With that said we can resolve the deprecation issue quite easily. Simply replace usages of get_woocommerce_term_meta() with the new native WordPress function get_term_meta().
get_woocommerce_term_meta() will simply pass its arguments on to the new function anyway so we can be sure the problem isn't with the deprecated function.
Likely causes of the issue:
Key used to save the image isn't the same as the key being used to retrieve
Not passing in correct term ID
No image set

Passing arguments to WordPress feed URL

As we all know the WordPress' feed url is www.mysite.com/feed.
I have edited the feed-rss2.php file to show thumbnails if a certain GET parameter is passed. See the code below:
<?php if($_GET['c'] == 'detailswiththumb') echo the_post_thumbnail( array(100,100) ); ?>
But when I open the feed address like this:
www.mysite.com/feed?c=detailswiththumb
The code doesn't work. Can the arguments be passed this way? Am I missing something? Please help.
Firstly, the function is get_the_post_thumbnail() not the_post_thumbnail().
Then, their is one more problem in your code, that you have to pass the post id to get its thumbnail (for more info see http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail).
So, first you will have to extract the post id from somewhere and then only you would be able to get the thumbnail. But, I think it would be very tough, so try giving up this thought, for it would take you a lot of time and no living being is going to access that path.
There were browser cache issues. Even with Google Chrome's incognito window. Had to test it with passing fake arguments like...
www.mysite.com/feed?c=detailswiththumb&fakearguments=123
...to clear the cache. And the code is fine.
Sorry for wasting your time guys.

Drupal Views - How to return information about a view that is being rendered

I'm having trouble dealing with hooks for the Views module in Drupal. What I'm trying to do is determine which view is being rendered so that I can identify it and make changes to it. IOW, I don't know ahead of time which view I'm working on.
In the code below, I've replaced my actual module name with "MODULENAME".
In my .module file I have included a file MODULENAME.views.inc file with the following:
include_once ( dirname(__FILE__) . '/MODULENAME.views.inc');
In the .views.inc file, I have a MODULENAME_views_api function like this:
function MODULENAME_views_api() {
return array('api' => 2, 'path'=> drupal_get_path('module', 'MODULENAME'),
);
}
Those seem to work just fine. So, now I try to get down to business with an actual hook...
function MODULENAME_views_pre_render(&$view) {
$get_view_info = $view->name;
echo $get_view_info;
}
MODULENAME_views_pre_render();
The problem is this throws an error, "Missing argument 1 for MODULENAME_views_pre_render().
So, obviously it expects me to pass in an identifier of some sort to tell it which view I want. But that's the whole point of this function is to determine which view is being rendered. If I knew the answer to that, then I wouldn't need to call the function in the first place.
Am I missing something obvious? Is there a function call that I can use to return this identifier?
You hook into things by implementing hooks, so this part of your code is ok:
<?php
function MODULENAME_views_pre_render(&$view) {
$get_view_info = $view->name;
echo $get_view_info;
}
But this:
<?php
MODULENAME_views_pre_render();
Why? You're not generating a view, Views is. It's not your job to invoke the hook. You just implement it.
So, you need to make changes to the view? you do it right there:
<?php
function MODULENAME_views_pre_render(&$view) {
if ($view->name == 'TheViewIWantToModify') {
// Make some changes to the $view
}
}
And that's it.
Also, note that depending on the type of modifications you want to do, you might want to implement another hook instead of hook_views_pre_render(). Take a look at the docs/docs.php file that comes with Views (version 6.x-2.12 at least, I don't know which version you have, and BTW you should indicate this) and starting on like 538 you'll see a few hook_views_pre_ and hook_views_post_ type of hooks (that is, their descriptions, for you to know what each of them are good for), and then you can decide which one to implement in your module.

wordpress plugins - handling variables

I wrote a simple twitter connect plugin which needs to display the 'logged in' user's name in the header after he logs in. The function is working properly but I am unable to display the $user variable in my header or anywhere outside the function even though it is assigned global.
Here is the end of the login function:
$user= $Twitter->get_accountVerify_credentials();
print_r($user);
// show screen name (not real name)
$twitter_user = $user->screen_name;
// show profile image url
$twitter_image = $user->profile_image_url;
I can see that it is successful because the $user gets printed, but when I call it in my header.php file the same way I can an error: Notice: Undefined variable: user
Any suggestions?
As opposed to getting wrapped up in the scope of the variables, I'd write a function to get this information. Something like:
function get_twitter_user_name(){
$user= $Twitter->get_accountVerify_credentials();
return $user->screen_name;
}
Then, in the header, where I want to display the name, I'd call the function like so:
<?php echo get_twitter_user_name(); ?>
Depending on the structure of your code, this may look a little different than what I have here, but hopefully this will give you another way of tackling this problem.

Do WordPress widget (or sidebar) hooks exist?

I'm trying to filter ALL widget output through a simple filter, but can't find any hooks and was hoping to be pointed in the right direction. Or possibly my efforts are not even possible?
My simple filter is something like this:
function clean_widget_output( $input ) {
return str_replace( array( "\t", "\n", "\r" ), '', $input );
}
add_[FILTER OR ACTION]( 'need_a_hook', 'clean_widget_output', 99 );
Any ideas? I'm pretty new to PHP, but I can get around.
This was borne out of the need/desire to clean the god-awful HTML spewed by WordPress' widgets. I love what they do, but some of the output makes me cry.
The short answer is output buffering because I couldn't find any widget or sidebar hooks.
The long answer is:
function tidy_sidebar( $sidebar_name_or_id )
{
ob_start();
$bool = dynamic_sidebar( $sidebar_name_or_id);
if ( $bool )
{
$str = ob_get_contents();
$str = 'do cleanup stuff...';
}
else
{
$str = '';
}
ob_end_clean();
return $str;
}
Then call echo tidy_sidebar( 'sidebar-name-or-id' ); from your theme.
I had a similar issue and after looking through Adam Brown's list of all WordPress filter hooks, found that the hook I needed does exist (widget_title, as pxl mentions), but that there is no hook to get all widget output. I thought I'd elaborate on the solution that worked for me.
Theoretically, the widget_title hook should affect all widgets on your blog, but I'm sure some 3rd party widgets neglect to include the necessary line of code to apply any title filters, so it's not foolproof. It worked for me, however, and it can be used to apply custom 'shortcode' (more accurately, in this case, 'longcode') or syntaxes to your widget titles. For example, I wanted to occasionally include html code in my widget titles, but by default, all html is stripped out. So, in order to be able to add things like <em> tags to text in some of my titles, I chose a custom syntax: [[ instead of < & ]] instead of > (for ex, [[em]] and [[/em]]) and then created a function in my theme's functions.php file to process that custom syntax and replace it with the html equivalent:
function parse_html_widget_title( $text ) {
return str_replace(array('[[', ']]'), array('<', '>'), $text);
}
Then I added a line below it to add the function as a filter:
add_filter('widget_title', 'parse_html_widget_title', 11); // 11 is one above the default priority of 10, meaning it will occur after any other default widget_title filters
The add_filter / apply_filter functionality automatically passes the content being filtered as the first parameter to the function specified as the filter, so that's all you need to do.
In order to do something similar for the main output of the widget, you would need to look at all your widgets to see what hook they use and verify that they have a filter for their main output, than use add_filter() for each hook you find with your custom callback function (for example, it's widget_text for the Text widget output, or get_search_form for the search form [you can see it in wp-includes/general-template.php, at the get_search_form() function]). The problem is that some of the dynamically generated widgets don't have hooks (like the Meta widget), which is why the output buffering solution Jeff provides is the most versatile, though not ideal, solution.
there are lots of hooks for wordpress widgets that aren't documented. The wordpress codex doesn't list them, for whichever reason (such as these hooks may change in the future and will break unexpectedly with new updates and versions)... so use these with extreme caution.
to find out what they are, there are at least 2 places to look:
<wordpress install directory>/wp-includes/default-filters.php
<wordpress install directory>/wp-includes/default-widgets.php
contained in those two files is a pretty good listing of all the hooks wordpress uses.
An example would be a filter for widgets is widget_title
again, use these with caution, they're not guaranteed to work past the specific version of the code you're looking at.
I'm not sure when they introduced the widget_text filter, maybe they didn't have it in '09 when this question was originally asked, but since it's there now, and for the sake of anyone that gets this stackoverflow like I did from google and just happens to read far enough down to see this answer, it's now actually quite simple:
function my_widget_filter( $content )
{
// manipulate $content as you see fit
return $content;
}
add_filter( 'widget_text', 'my_widget_filter', 99 );
Also maybe check out the dynamic_sidebar_params filter --
Another link --

Resources