I am trying to customise a title for a Wordpress page. In the page I have some php to obtain data from the database. I would like to output a custom title based on the data and the page id. I have tried this:
add_filter('wpseo_title', 'property_title', 10, 1);
function property_title() {
global $post;
$postid = $post->ID;
if ($postid == '72616') {
$title['title'] = "Property number $propertyid";
return $title;
}
}
How can I check the postid and if it is the right page, output the data from the sql query?
Many thanks,
Neil.
The title is passed in for that filter. Make sure you return title outside of the if statement.
add_filter('wpseo_title', 'property_title', 10, 1);
function property_title($title) {
global $post;
$propertyid = get_query_var('reference');
$postid = $post->ID;
if ($postid == '72616') {
$title = "Property number $propertyid";
}
return $title;
}
add_filter( 'query_vars', 'add_property_query_vars' );
function add_property_query_vars( $query_vars ) {
$query_vars[] = 'reference';
return $query_vars;
}
Related
I want to execute a function in a specific page only, that edits the post title of posts inside a post-grid plugin.
I tried:
if (is_page('my-slug')) { add_filter(...); } //page slug
if (is_page('12345')) { add_filter(...); } //page ID
add_filter('the_title', 'title_with_attributes', 10, 2);
function title_with_attributes($title, $id) {
if (is_page('12345') || is_page('my-slug')) {
//Do stuff ...
}
}
With no results. The function is the following and runs fine.
function title_with_attributes($title, $id) {
global $wp_query;
if(get_post_type($post->ID)=="product"){
global $post;
//get other variables from post here ...
$title = "add text and vars ".$title;
}
return $title;
}
Maybe there is a way to add this function when the plugin runs but I do not know how. The plugin is the "Post-Grid" from WP Bakery
I'm trying to customize a shortcode put inside a custom plugin, but I can't get the user id, it always returns me 0.
It might also be okay to understand the role with current_user_can, but any information is always empty.
Here the code:
add_action( 'plugins_loaded', 'check_current_user' );
function check_current_user() {
// Your CODE with user data
global $current_user;
$current_user = wp_get_current_user();
return $current_user->ID;
}
function appp_hide_content_shortcode( $atts, $content = '' ) {
if( class_exists('AppPresser') && AppPresser::is_app() )
return check_current_user();
else
return $content;
}
add_shortcode('appp_hide_content', 'appp_hide_content_shortcode');
Please try this
/**
* Hide content on app.
*
* Use this shortcode to hide content when viewed using the app.
*
* Use:
* [appp_hide_content]This content will not appear on the app.[/appp_hide_content]
*/
function appp_hide_content_shortcode( $atts, $content = '' ) {
ob_start();
//GET CURRENT USER ID
global $current_user;
$current_user = wp_get_current_user();
echo $current_user->ID;
if (current_user_can('free')){
if( class_exists('AppPresser') && AppPresser::is_app() )
echo '';
else
echo $content;
}
$sc_html = ob_get_contents();
ob_end_clean();
return $sc_html;
}
add_shortcode('appp_hide_content', 'appp_hide_content_shortcode');
You need to check the user after the user is loaded. I would hook to init. You can't call the pluggable functions directly in a plugin without delaying the execution. The simpler solution to your problem would be to include your shortcode in your theme's functions.php, rather than in a plugin. But below will execute.
add_action( 'init', 'check_current_user' , 999);
function check_current_user() {
// This returns current user id
return get_current_user_id();
}
function appp_hide_content_shortcode( $atts, $content = '' ) {
if( class_exists('AppPresser') && AppPresser::is_app() ){
// This is only returning an integer here.
return check_current_user();
} else {
return $content;
}
}
add_shortcode('appp_hide_content', 'appp_hide_content_shortcode');
I want to use a filter hook to add an image before titles of my pages, but it also adds them to my menu titles. How can I get around this?
function scissorTitle ($title)
{
$scissortitle .= "<img src='*image_link*'>" . $title;
return $scissortitle;
}
add_filter( 'title', 'scissorTitle', 10);
The filter title has two parameters $title and $id you can use $id to change the only specific title of the page Check docs. Your full code should look like this:
function scissorTitle ($title, $id) {
global $post;
if (is_singular() && $post->ID == $id) {
$title = '<img src="image_link">' . $title;
}
return $title;
}
add_filter( 'the_title', 'scissorTitle', 10, 2);
I am having hard time excluding menu items from the_filter function I have to modify the post titles.
I want to modify the single post title as well as the posts in my sidebar, using in_the_loop() is working fine but it is also excluding my sidebar/archive post titles, breadcrumbs etc.
is there a way to detect menu items and exclude them? other then using in_the_loop()
here is my code:
function update_the_title( $title, $post_id ) {
if (not menu items) {
return $title + 'some string';
} else {
return $title;
}
}
add_filter( 'the_title', 'update_the_title');
Thanks
you have to try like this
Not tested
function update_the_title( $title, $post_id ) {
$menuLocations = get_nav_menu_locations();
$menuID = $menuLocations['primary']; // Get the *primary* menu ID
$primaryNav = wp_get_nav_menu_items($menuID);
if (array_search($post_id, array_column($primaryNav, 'ID'))) {
return $title + 'some string';
} else {
return $title;
}
}
add_filter( 'the_title', 'update_the_title', 10, 2);
In my wordpress theme i have included meta box for Title, description and redirect section. I have a input field for the redirect box under every post and page id. If i add any url to a particular page/post id for that redirect box, the particular page/post id should redirect to my own url given in that redirect box. Is there any function to do this? I have searched in stackies and i get the following code. But its not working.
function my_permalink_redirect($permalink) {
global $post;
if ($post->ID == your_post_id_here) {
$permalink = 'http://new-url.com/pagename';
}
return $permalink;
}
add_filter('get_the_permalink','my_permalink_redirect');
Try:
add_filter( 'the_permalink', 'filter_function_name_7062', 10, 2 );
function filter_function_name_7062( $permalink, $post ){
global $post;
if ($post->ID == 684) {
$permalink = 'http://sample.com/1';
}
if ($post->ID == 444) {
$permalink = 'http://sample.com/2';
}
return $permalink;
}
Try:
function my_permalink_redirect($permalink) {
global $post;
if ($post->ID == your_post_id_here) {
$permalink = 'http://new-url.com/pagename';
wp_redirect("'.$permalink.'", 301);
exit;
}
}
add_filter('get_the_permalink','my_permalink_redirect');