Custom post type functions.php if statement on action - wordpress

I am using developing a child theme for Woothemes' Canvas.
I am trying to use functions.php in the child theme to only use actions on my custom post type.
This code doesn't seem to be working:
add_action( 'woo_post_inside_after', 'my_geo_mashup' );
function my_geo_mashup() {
echo GeoMashup::map();
if ($post->post_type == 'listings') {
//My function
}
}
add_action( 'woo_post_inside_before', 'listings_nivo' );
function listings_nivo() {
echo do_shortcode('[nivo source="current-post" ]');
if ($post->post_type == 'listings') {
//My function
}
}
So, I'm unsure how to get the above to work properly and only show these items on the custom post type, or only for the custom post type template single-listings.php (as I only want the map and slider to show on the actual post, not on the blog page (archive.php)

Rather than making the entire $post object global, you can just make $post_type global instead. Ex below.
I'm not exactly sure where that function is being loaded, but make sure you hook somewhere within the post. If the action is before, as far as I know and from experience, the post variable will be null.
Just as a test, try running the action in wp_footer Ex. add_action( 'wp_footer', 'listings_nivo' );
See if that yeilds any results.
if echoing var_dump($post) is still null, well, not sure where to go from there.
So you can try running the below, then run the action in the appropriate place if it works:
function listings_nivo() {
echo do_shortcode('[nivo source="current-post" ]');
global $post_type;
// Diagnostic purposes
echo var_dump($post_type);
if ($post_type == 'listings') {
//My function
}
}
add_action( 'wp_footer', 'listings_nivo' );
Check your error log or turn wp_debug to true in your wp-config.php file if nothing else to see if anything else is going on.
Best of luck!

Inside your function, try adding global $post;. Then to see what you are getting with $post->post_type echo it out to the screen. As long as this gives you "listings", your code should work. If not, there's probably another issue at play.

Related

$wp_query is returning nothing

I have the following code in my plugin file:
// SET UP REWITE RULES FOR LISTING PERMALINKS //
function my_rewrite_tags() {
add_rewrite_tag('%listingId%', '([^&]+)');
}
add_action('init', 'my_rewrite_tags', 10, 0);
function my_rewrite_rules() {
add_rewrite_rule('^listing/([^/]*)/?','index.php?pagename=listing&listingId=$matches[1]','top');
}
add_action('init', 'my_rewrite_rules', 10, 0);
This idea is that I have a page called "Listing" with the permalink "listing" and I want to be able to have the listing's ID number after it (i.e., /listing/12345/)
I then have a shortcode running on the "Listing" page
// SHORTCODE FOR SINGLE LISTING PAGE //
function my_single_listing(){
ob_start();
include('templates/single-listing.php');
return ob_get_clean();
}
add_shortcode('listing','my_single_listing');
...and the first thing it does is try to get that listing ID with the code:
$listingId = $wp_query->query_vars['listingId'];
I've done this with other plugins I've written in the past, but in this case it's decided to not work. In fact, if I enter the code:
print_r($wp_query);
I get absolutely nothing returned from it at all. (All other content on the page is displaying fine though.)
Any ideas?
Your issue with $wp_query being blank might be due to it not being accessed as a global variable. Prefacing it with a global declaration will allow it to access the global query:
global $wp_query;
print_r( $wp_query )
The issue with the listing ID not being picked up has to do with it not being declared as a possible custom query var. WordPress requires you to declare them before it loads them into the global wp_query for the page (presumably for security). $_GET was able to access them since that bypasses WordPress and just uses it with PHP.
function so_71685702_add_query_vars( $query_vars ) {
$query_vars[] = 'listingId';
return $qvars;
}
add_filter( 'query_vars', 'so_71685702_add_query_vars' );
Once you've got that, $wp_query->query_vars( 'listingid' ) should return a value.
Here's the query_vars hook information page, and the get_query_var hook information page which might be useful for further reading - might cover some things you'll run into based on the way you're setting up custom rewrites and query vars.

Where exactly is the "Before Header Content hook" in Wordpress?

I want to add a little code to the "Before Header Content hook" but I don't know where that is... Can you please help me?
Try:
add_action('init', 'process_post');
function process_post()
{
echo "test";
}
this is a modified version of #ajay 's solution
if you are going to use this, then you have to make sure that the current user is not an admin using is_admin() function.. and only display it if he is not an admin ...
why !?
because if you didn't, it may mess up the wp-admin of your website..
add_action('init', 'process_post');
function process_post()
{
if (!is_admin()) {
echo "test";
}
}
This could be tricky simply because every theme differs with how the loop is displayed, however you could create a plugin to use the loop_start action, which is called before the first post of the standard WP loop:
add_action( 'loop_start', 'test_loop_start' );
function test_loop_start( $query ){
echo 'this is my inserted text';
}
Now using this would display it every single time the loop is called (whether on a page, a post, category page, search page, etc.), which you may not want.
add_action( 'loop_start', 'test_loop_start' );
function test_loop_start( $query ){
if(is_category() OR is_singular()) {
echo 'this is my inserted text';
}
}

WooCommerce Registration Shortcode - Error messages problems

I am currently creating a widget to display the registration form on a WordPress website that uses WooCommerce. For now, I only have 3 basic fields which are email, password, repeat password. I'm looking forward to add more WooCommerce fields, but want to solve that problem before jumping to the next step.
I'm having some problems with the messages output (wrong password, account already exists, etc).
I searched on the web and there was no shortcode already built for WooCommerce registration, beside their registration page. So I went ahead and created a shortcode, with a template part.
function custom_register_shortcode( $atts, $content ){
global $woocommerce;
$form = load_template_part('framework/views/register-form');
return $form;
}
add_shortcode( 'register', 'custom_register_shortcode' );
This is a snippet I use to get the template part inside a variable, since the default function would "echo" the content instead of "returning" it.
function load_template_part($template_name, $part_name=null) {
ob_start();
get_template_part($template_name, $part_name);
$var = ob_get_contents();
ob_end_clean();
return $var;
}
So, the problem is, when I call woocommerce_show_messages or $woocommerce->show_messages(); from my template part, nothing is showing, or if it is, it shows at the top of the page.
I did try to put the calls inside my shortcode function:
function custom_register_shortcode( $atts, $content ){
global $woocommerce;
$woocommerce->show_messages();
$form = load_template_part('framework/views/register-form');
return $form;
}
add_shortcode( 'register', 'custom_register_shortcode' );
Doing so, the message output inside the <head> tag, which is not what I want.
I tried to do the same trick with ob_start(), ob_get_contents() and ob_clean() but nothing would show. The variable would be empty.
I also did try to hook the woocommerce_show_messages to an action as saw in the core:
add_action( 'woocommerce_before_shop_loop', 'woocommerce_show_messages', 10 );
For something like:
add_action( 'before_registration_form', 'woocommerce_show_messages');
And I added this in my template-part:
<?php do_action('before_registration_form'); ?>
But I still can't manage to get the error messages show inside the box. It would always be inserted in the <head>
I will share final solution when everything is done.
Thanks for your time,
Julien
I finally got this working by hooking a custom function to an action which is called in my header.php
I guess hooking functions inside template part does not work as intended.
In header.php, I got this:
do_action('theme_after_header');
And here's the hooked function. Works perfectly.
function theme_show_messages(){
woocommerce_show_messages();
}
add_action('theme_after_header', 'theme_show_messages');
However, I will look into 'unhooking' the original show message function since it might show twice. Need to test some more ;)
You can also just use the [woocommerce_messages] shortcode in your template where you want it displayed
Replying to a bit of an old question, but you can also try the following:
$message = apply_filters( 'woocommerce_my_account_message', '' );
if ( ! empty( $message ) ) {
wc_add_notice( $message );
}

Wordpress Plugin: Show html only on standard page and not in admin area

I'm writing a plugin and I need to display a piece of text in the WP page, but not in the admin area. How can I do so?
I tried this in the construct:
add_action( 'init', array( $this, 'initPage' ) )
and then:
public function initPage() {
echo 'hello';
}
but the text is displayed also in the admin area. Is there a way to do this? It would be the opposite of the action admin_init I assume.
Proper way to handle it: is_admin()
http://codex.wordpress.org/Function_Reference/is_admin
if(is_admin()) { // do nothing } else {
// function you want to execute.
}
I solved this by adding it to a shortcode action. Like this:
add_shortcode( 'myPlugin', array( $this, 'shortcode' ) );
and:
public function shortcode( $atts ) {
return 'hello';
}
With the above code, 'hello' will only display on the front-end. Not sure if that's the cleaner way to do it, but does the job.
There is no "front-end-only" version of init, however you probably don't want to be doing any output at the init action anyway.
What exactly are you trying to do? Usually, you use an action hook for specific types of things, and causing output very early at something like "init" is rare and weird.

Running a function in Wordpress when publishing a custom post type

I am trying to run the following function anytime a wordpress "Jobs" custom post is published. The code works (lines 2-7) when it is placed in my theme template, but it only runs when the post is viewed. I want the code to run when the post is published, and so I have tried adding the code in a function inside my functions.php, but nothing is happening when each custom post is published. Any suggestions?
function indeedgeo(){
$indeedgeo = get_post_meta($post>ID, indeedgeo, true);
$indeedgeos=explode(' ',$indeedgeo);
$_jr_geo_latitude = $indeedgeos[0];
$_jr_geo_longitude = $indeedgeos[1];
update_post_meta($post->ID, _jr_geo_latitude, $_jr_geo_latitude);
update_post_meta($post->ID, _jr_geo_longitude, $_jr_geo_longitude);
}
add_action('publish_Jobs', 'indeedgeo');
You should hook into one of the three actions;
do_action('edit_post', $post_id, $post);
do_action('save_post', $post_id, $post);
do_action('wp_insert_post', $post_id, $post);
that are run when a post is either saved, or has its status updated. Something like the following should do the trick.
function se_10441543_save_post($post_id, $post){
//determine post type
if(get_post_type( $post_id ) == 'your_post_type'){
//run your code
$indeedgeo = get_post_meta($post_id, indeedgeo, true);
$indeedgeos=explode(' ',$indeedgeo);
$_jr_geo_latitude = $indeedgeos[0];
$_jr_geo_longitude = $indeedgeos[1];
update_post_meta($post_id, _jr_geo_latitude, $_jr_geo_latitude);
update_post_meta($post_id, _jr_geo_longitude, $_jr_geo_longitude);
}
}
add_action('save_post', 'se_10441543_save_post', 10, 2);
http://codex.wordpress.org/Plugin_API/Action_Reference
Not exactly sure how your "publish_jobs" hook is working but right off the bat, if you are placing this function in your functions.php, you will need to give it context. Replace $post>ID with the post number (an integer). If this applies to many post you will probably want to use another method of querying post data: http://codex.wordpress.org/Class_Reference/WP_Query. Let me know if this helps.

Resources