recently I made a question about content filtering which went ok.
Now I'm trying to achieve the same filtering about title, but when it comes to title it doesnt filter only the_title(); but all titles within wp_nav are being filtered.
The aim is to achieve filtering single post title not the ones within the loop (I know about in_the_loop() )
<?php
class Filter_Title {
public function __construct() {
if( !is_front_page() && !is_home() && !is_single() ) return;
if( !is_singular( array('post','page') ) ) return;
add_filter( 'the_title', array(&$this, 'manage_page_title') );
}
public function manage_page_title($title) {
$title = '';
return $title;
}
}
$filtertitle = new Filter_Title();
?>
This is my mini plugin class.
I just tested this and you can match the post_ID with a param to check if its a post.
Since post ids don't match menu ids this should work. Give it a try.
function remove_post_title($title, $id) {
if (is_single() && in_the_loop() && $id == get_the_ID()) {
$title = '';
}
return $title;
}
add_filter('the_title', 'remove_post_title', 10, 2);
Note: in a plugin you may need to hook the add_filter in another function and run it in wp_head like before.
Related
I'm trying to build custom export column in WP All Export but I can not get what I'm trying to achieve. I need column 'For Payment', where I need to place actual order total in case the payment method is COD. Otherwise it should return 0 (required by couriers). I tried to modify code below but it doesn't work.
<?php
function cod_payment( $order_id ) {
$total = get_total();
$method = get_payment_method();
if($method == "cod") {
return $total;
} else {
return "0";
}
}
?>
You can do something like this. select Custom export field add the function call in the text area
[cod_payment({Payment Method},{Order Total})]
and finaly the PHP to the function editor (make sure to save it)
<?php
function cod_payment( $method, $total ) {
if($method == "cod") {
return $total;
} else {
return "0";
}
}
?>
Keep in mind the method name may not be correct.
I need column 'For Payment'
So the value of the Column name text field should be "For Payment", not "Order Total".
Also, in order for the PHP function to be called, you need to specify its name in the text field between <?php and ($value); ?>. It should be "cod_payment", not "cliente_jr".
Finally, why isn't the $order_id being used inside the function? I assume you need it to fetch the total and the payment method, possibly something like this:
<?php
function cod_payment( $order_id ) {
$total = get_total( $order_id );
$method = get_payment_method( $order_id );
if($method == "cod") {
return $total;
} else {
return "0";
}
}
?>
I tried several codes to disable the price in product variable in woocommerce.
I've tried this but nothing happened:
/*
Disable Variable Product Price Range completely:
*/
add_filter( 'woocommerce_variable_sale_price_html','my_remove_variation_price', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'my_remove_variation_price', 10, 2 );
function my_remove_variation_price( $price ) {
$price = '';
return $price;
}
I put it in functions.php but nothing happened.
My theme is saha from theme-junkie.
Best way would be to hook into woocommerce_get_price_html filter.
It passes $price HTML and WC_Product* object. You can check if it is instance of WC_Product_Variable to determine if it is a variable product. Then just return '' empty string if it is. This will handle the price display for parent Variable product.
Example
add_filter('woocommerce_get_price_html', 'mm_handle_variation_prices', 10, 2);
function mm_handle_variation_prices( $price_html, $product_obj ){
// Bail unless we are on WooCommerce page on frontend and this is an ajax request.
if( is_admin() || is_ajax() || ! is_woocommerce() ) {
return $price_html;
}
// If this is an Variable product only then return empty string.
// '$product_obj instanceof WC_Product_Variable' check would work as well.
if( 'variable' === $product_obj->get_tyepe() ) {
return '';
} else {
return $price_html;
}
}
The Challenge
I have been trying to write a filter that applies (prepends a string) to (a) post titles of a certain post type and (b) to that same post type WordPress admin backend for use by an auto social sharing plugin.
I've gotten really close. I've achieved:
1. prepending only to custom post_type post titles
2. prepending to custom_post type titles AND all nav menu items
3. prepending to ALL titles (post, page, nav menu, and backend auto-sharing plugin)
The final, 3rd attempt got me very hopeful, but yet still, no matter the function, conditional statement, or combination I use, I can't get custom_post title AND backend social sharing plugin, but not nav menu items.
My Code - what I've tried
I've tried different conditionals, like is_admin() and !is_nav_menu with all nav menu IDs. I've also tried comparing the ID of the post to the ID of the menu. Finally, I've also tried adding the filter only to loop_start and that seems promising if combined with other statements. I will clean up my code for your review but I leave it this way for now in hopes it helps to see what I've tried and maybe where I went wrong with any of those methods.
// , $id = NULL
function append_album_review_to_title( $title ) {
global $post;
global $dont_apply_title_filter;
$text = 'Album Review: ';
$prepended_title = $text . $title;
/*
$nav_menus_obj = wp_get_nav_menus();
$nav_menu_ids = '';
foreach( $nav_menus_obj as $nav_menu ) {
$nav_menu_ids .= $nav_menu->term_id . ',';
}
rtrim($nav_menu_ids, ',');
*/
// if ( get_post_type( $post->ID ) == 'album_review' && in_the_loop() ){
// if ( get_post_type( $post->ID ) == 'album_review' && $id == get_the_ID() ){
// if ( !is_nav_menu( '32057,32058,35135,32054,32056' ) ) {
// if ( get_post_type( $post->ID ) == 'album_review' && is_nav_menu( $id ) ) {
if ( get_post_type( $post->ID ) == 'album_review' && !$dont_apply_title_filter ) {
//print_r( $nav_menu_ids );
//print_r( is_nav_menu( $nav_menu_ids ) );
return $prepended_title;
/*
} elseif ( get_post_type( $post->ID ) == 'album_review' ) {
return $title;
*/
} else {
return $title;
};
}
add_filter('the_title', 'append_album_review_to_title');
//add_action('save_post', 'custom_post_type_title', 100);
/*
function set_custom_title() {
add_filter( 'the_title', 'append_album_review_to_title', 10, 2 );
}
add_action( 'loop_start', 'set_custom_title' );
*/
Solution
function append_album_review_to_title( $title, $id = NULL ) {
if ($id) {
if ( get_post_type( $id ) == 'album_review' ){
return 'Album Review: ' . $title;
} else {
return $title;
}
} else {
return 'Album Review: ' . $title;
};
}
add_filter('the_title', 'append_album_review_to_title', 10, 2);
Explanation
First trying this:
function append_album_review_to_title( $title, $id ) {
if ( get_post_type( $id ) == 'album_review' ){
return 'Album Review: ' . $title;
} else {
return $title;
}
}
add_filter('the_title', 'append_album_review_to_title', 10, 2);
I noticed that on the backend, the social sharing plugin I am using to auto-share posts would return warnings like missing parameter 2 and Notice: Trying to get property of non-object..., and so it occurred to me that unlike the front end (nav menu and the loop), in the backend, apparently an $id is not being passed to the_title and so I can use this as a conditional.
Checking for $id will, for my purposes, tell me that if it is true, we are on front end, if it is false, then we are at backend post view.
This code accomplishes what I need (ie, modify post title in the loop, do NOT modify nav menu items, and modify the_title for use by a backend social sharing plugin):
I created a filter that modifies the_title() of a post, but the problem I'm having is that it's modifying every instance of the_title(). I got most of the problem sorted out with the in_the_loop() function, however any theme that has "next post" "previous post" navigation links within the loop are still having the filter applied (understandably so). How can I apply the filter to only the the_title() of the current post?
function xyz_the_title( $the_title ) {
if( !in_the_loop() )
return $the_title;
$location = get_post_meta( get_the_ID(), 'location', true );
$the_title .= ' - ' . $location;
return $the_title;
}
add_filter( 'the_title', 'xyz_the_title' );
do it with jQuery
Something like this should do the trick:
$(document).ready(function() {
$('#entry-header').text(function(i, oldText) {
return oldText === 'Popular Science' ? 'New word' : oldText;
});
});
This only replaces the content when it is Popular Science. See text in the jQuery API.
Rather than filtering the_title you could edit your template file instead and append the location to your returned the_title().
echo "<h1>" . get_the_title() . " - " . $location . "</h1>";
Ran into a similar situation and was hoping this thread could save me...
Anyways this is what I managed to do thus far in
add_filter( 'the_title', function( $title, $id ){
/**
* don't run in the backend
*/
if( is_admin() ) {
return $title;
}
/**
* invalid values received
*/
if( empty( $title ) || $id < 1 ){
return $title;
}
global $post;
if ( ! $post instanceof WP_Post ){
return $title;
}
/**
* PREVENTATIVE MEASURE...
* only apply the filter to the current page's title,
* and not to the other title's on the current page
*/
global $wp_query;
if( $id !== $wp_query->queried_object_id ){
return $title;
}
/**
* Don't run this filter if wp_head calls it
*/
if( doing_action( 'wp_head' ) ){
return $title;
}
return 'MODIFIED - '.$title;
});
Here are the shortfalls:
If you have the same post being displayed on the current page, somewhere else, it's gonna modify THAT post title as well
Currently thinking about having a look at the call stack to detect if the call is coming from the theme...
but I would suggest you find another solution bud...
Ah, didn't know it was for a plugin. In which case, I think you should be ok to use if_filter then. This checks whether the filter has been run x times on the page in question. So, we check if it has run once on the page and if so it won't run again. Also, I have assumed you only want this to run on single post pages. This is untested.
function xyz_the_title( $the_title ) {
if( is_single() AND did_filter('the_title') === 1 ) {
if( !in_the_loop() )
return $the_title;
$location = get_post_meta( get_the_ID(), 'location', true );
$the_title .= ' - ' . $location;
return $the_title;
}
}
add_filter( 'the_title', 'xyz_the_title' );
i created a custom page named custom_page.php in wordpress and use this as a page template.
i wanted to create my own functions and only target my custom_page.php. this function will be installed as a plugin.
what kind of WP HOOKS should i use, for example i have this code below
function your_function() {
if ( is_page_template('custom_page.php') ) {
echo '<p>This is inserted at the bottom</p>';
{
}
add_action('wp_footer', 'your_function');
i want to only execute this code in my custom_page.php footer area.
[EDIT]
You can use these hooks for page templates.
http://adambrown.info/p/wp_hooks/hook/page_template
Or may be something like this...
add_filter( 'page_template', 'check_the_template' );
function check_the_template( $template ) {
if( 'custom_page.php' == basename( $template ) ) {
// do some things here by calling functions, hooks etc..
}
return $template;
}
If you want to just echo "Hello World" :
add_filter( 'page_template', 'check_the_template' );
function check_the_template( $template ) {
if( 'pagetemplete.php' == basename( $template ) ) {
add_action( 'the_content', 'echofunction' );
}
return $template;
}
function echofunction() {
echo "Hello World";
}