WP_Query in functions.php

Simple, you’re addressing $post out of context. When you’re running a standard WordPress loop, WP will load a global $post variable with the results of the current query. This includes the ID, title, content, post meta, etc. The loop functions will reference this global variable to actually give you data. Take the regular function get_the_ID() … Read more

Display custom comments field for first level only

The Reply links I assume your Reply links look like this: <a class=”comment-reply-link” href=”http://wordpress.stackexchange.com/2013/12/29/hello-world/?replytocom=32#respond” onclick=”return addComment.moveForm(‘comment-32′, ’32’, ‘respond’, ‘1’)”>Reply</a> and the Cancel Reply link: <a rel=”nofollow” id=”cancel-comment-reply-link” href=”http://wordpress.stackexchange.com/2013/12/29/hello-world/#respond” style=””>Cancel reply</a> The Javascript method addComment.moveForm is defined in /wp-includes/js/comment-reply.min.js. Demo plugin: To hide the extra Subject field for comments replies, you can try to check for … Read more

Can’t get post ID in functions.php?

The post ID is available after the query has been fired. The first hook that is safe to get post id is ‘template_redirect’. If you can modify your function to accept a post id as argument, like so: function em_change_form($id){ $reg_type = filter_input(INPUT_GET, ‘reg_typ’, FILTER_SANITIZE_STRING); if($reg_type === ‘vln’){ update_post_meta($id,’custom_booking_form’, 2); } elseif ($reg_type == ‘rsvp’) … Read more

exclude multiple terms using get_terms() function

With get_terms(), the exclude parameter takes an array of term IDs, so just add the second term to the array: $terms = get_terms( TribeEvents::TAXONOMY, array( ‘orderby’ => ‘name’, ‘order’ => ‘ASC’, ‘exclude’ => array( 77, 71 ), ) ); echo ‘<li>Category:</li>’; foreach ( $terms as $term ) { echo ‘<li><a href=”‘.$url.’?tribe_eventcategory=’.$term->term_taxonomy_id.'”>’.$term->name.'</a></li>’; }

Auto close (hide) custom metabox / set default state

Hook into postbox_classes. postbox_classes is the function which will output the classes for the metabox. apply_filters( “postbox_classes_{$page}_{$id}”, $classes ) Your code could look like this: add_action( ‘add_meta_boxes’, ‘add_my_metabox’ ); function add_my_metabox() { $id = ‘my-metabox’; $title=”My Metabox”; $callback = ‘my_metabox_content’; $page=”post”; add_meta_box( $id, $title, $callback, $page ); add_filter( “postbox_classes_{$page}_{$id}”, ‘minify_my_metabox’ ); } function my_metabox_content() { … Read more

Using a private method as an action callback from within a class

It’s not possible to call a private method through an action or filter. When calling add_action or add_filter, WordPress adds the callback to a list of callbacks for that specific action or filter. Then, when do_action or apply_filters is called, WordPress uses call_user_func_array to call the linked functions and methods. As call_user_func_array is not called … Read more