Changing wp_title from inside my Wordpress Plugin - php

nearly finished my wp plugin for an estate agent,
I spidered the site for 404's etc, i noticed that my property details pages were spider'd in which all 45 page titles were : ( details | sitename ) (page titles are shown dynamicly from an ID being passed via querystring)
now I've got my nice urls fixed, urls look like this...
wpsite.com/details/20043/property+for+sale+in+this+area
In Which...
propid = 20043
propname = property+for+sale+in+this+area
both of these are querystring values which are used to re-write the urls.
'query_vars' => array('propid', 'propname'),
'rules' =>
array( '(.+?)/([^/]+)/([^/]+)/?$' => 'index.php?pagename=$matches[1]&propid=$matches[2]&propname=$matches[3]' )
);
Now when the property details page is loaded im trying to hook into the wordpress filter
wp_title but this isnt working the way i expected..
this is the code im using to generate the titles
function wp_myplugin_property_title()
{
$wp_acquaint_id = get_option("wp_system_id");
$propid = get_query_var('propid');
if(isset($propid)){
$seotitle = wp_myplugin_seo_title($propid);
}else{
$seotitle = "TEST Title";
}
return $seotitle;
}
if( is_page('details') ){
add_filter('wp_title', wp_myplugin_property_title, 100);
}
the function used within that function: wp_myplugin_seo_title($propid) generates the actual title i want to use...
function wp_myplugin_seo_title($propid)
{
$wp_acquaint_id = get_option("wp_acquaint_id");
$xml = wp_myplugin_get_property($propid);
foreach($xml->PropertiesDataSet->Properties as $node) {
include('xml_loop.php');
if($bedrooms==0){ }else{ $seo_title.= $bedrooms." bedroom "; }
$seo_title.= wp_myplugin_get_property_type($type_id)." "; //ie:flat
$seo_title.= str_replace("(","",$street);
$seo_title.= " ".$town." | ".get_bloginfo('name');
}
return $seo_title;
}
Im finding that with the if(is_page()) in place around the filter the page title dosnt change and if i remove the is_page, the prop details page title works fine but!!!
while on the property listing page the page title is looping through all the properties on that page and producing a page title around 1000 char long..!
I have been hunting around for a better way to deal with this but any help would be great..
Cheers
Marty
ps: currently running wordpress seo by Yoast!
thats why ive set the priority as 100 in the add_filter just to see if it would overwrite the title..

Using is_page in functions.php doesn't work, as it runs before wp knows what page it's going to render, or even if it's a page to begin with. Stick the is_page() inside the function, and it should work. Like:
function wp_myplugin_property_title()
{
if( is_page('details') ){
$wp_acquaint_id = get_option("wp_system_id");
$propid = get_query_var('propid');
if(isset($propid)){
$seotitle = wp_myplugin_seo_title($propid);
}else{
$seotitle = "TEST Title";
}
return $seotitle;
}
}
add_filter('wp_title', wp_myplugin_property_title, 100);

Related

WordPress - Yoast Disable specific page href link in breadcrumb trail, but don't remove text

Using Yoast to generate the breadcrumb trail, I can't figure out how to remove just the HREF attribute, so the breadcrumb is just text. I'm using custom links to organize the site content. I put in blank pages so that the breadcrumb trail would render properly, but now of course I have a link to blank pages. I've considered putting an index/TOC on these pages, but it seems so pointless.
Here's the simplest code that I've found that disables link and text, but I can't find an example (that works) that removes just the link (href).
add_filter( 'wpseo_breadcrumb_single_link' ,'wpseo_remove_breadcrumb_link', 10 ,2);
function wpseo_remove_breadcrumb_link( $link_output , $link ){
$text_to_remove = 'Products';
if( $link['text'] == $text_to_remove ) {
$link_output = '';
}
return $link_output;
}
You can try this:
add_filter( 'wpseo_breadcrumb_single_link' ,'wpseo_just_remove_breadcrumb_link', 10 ,2);
function wpseo_just_remove_breadcrumb_link( $link_output , $link ){
//In case you want to remove multiple links put those texts here in this array.
$text_to_remove = ['Products', 'Something else'];
if(in_array($link['text'] , $text_to_remove )) {
$link_output = str_replace('href="'.$link['url'].'"' , "" , $link_output);
return str_replace('data-wpel-link="internal"' , "" , $link_output);
}
return $link_output;
}

WordPress Pages - Adding subdirectory to permalink for specific pages

Currently, I have my permalink structure set up such as https://example.com/blog/%postname%/ which makes it so that whenever a blog post is loaded, the URL shows /blog/. I would also like to add the subdirectory /try/ for specific pages. I have tried to use a plugin to add categories to pages so I can create a category and add it that way, but it seems that this is not possible because the "Custom Structure" is already set as noted above.
Does anyone know of a way to add a subdirectory /try/ to specific pages for a WordPress website so the URL for certain pages would be such as https://example.com/try/page-title
Thanks.
First we have to add rewrite_rule
add_rewrite_rule(
'^try/([^/]+)/?',
'index.php?name=$matches[1]',
'top'
);
Then we need to use the post_link filter to change the link format for certain posts
add_filter( 'post_link', 'custom_post_permalink', 10, 4 );
function custom_post_permalink( $link, $post = 0 ) {
$post_cur_id = $post->ID;
$post_cur_slug = $post->post_name;
//array of post_ids with try in permalink
$try_array = array(1,7,22,78);
if(in_array($post_cur_id, $try_array)) {
$slug = '/try/' . $post_cur_slug . '/';
$link = home_url($slug, 'https');
}
return $link;
}
The array $try_array should contain post_id's in which we change the link format

How to replace text & URLs in shortcode output

I'm trying to add a custom function to my WordPress blog that will replace "bigalspets.com" with "bigalspets.ca" in all post text and/or hyperlinks, if the visitor's Geolocation is based in Canada.
If visitor IP = Canadian, replace "bigalspets.com" with "bigalspets.ca" in all rendered post content (including links and content generated by other plugins!).
I'm using the GeoIP Detection plugin to get GeoIP information, and the Code Snippets plugin to add custom functions that run only on the website's front-end.
The function I have so far is this:
function replace_if_canada( $text ) {
$geoip = geoip_detect2_get_info_from_current_ip();
if ( $geoip->country->isoCode == 'CA' ) {
return str_replace('bigalspets.com', 'bigalspets.ca', $text);
}
}
function replace_if_canada_shortcodes( $output ) {
$geoip = geoip_detect2_get_info_from_current_ip();
if ( $geoip->country->isoCode == 'CA' ) {
echo str_replace('bigalspets.com', 'bigalspets.ca', $output);
}
}
add_filter('the_content', 'replace_if_canada', 999);
add_filter('do_shortcode_tag', 'replace_if_canada_shortcodes', 999);
Here's a url of a WP post where the function should work:
https://eheimsupport.com/blog/themed-tanks/items/canyon-island/
THE PROBLEM: the 'replace_if_canada_shortcodes' part of my code isn't working i.e. the code replaces text/urls in normal post content but not in the Image Map Pro shortcode's output!
If you hover over one of the little "i" icons on the image map in the content body, you'll see product URLs. Those are not changing from .com to .ca...
How do I get the function to apply to EVERYTHING on ALL posts and pages. Please help! :(

How to catch a URL parameter and display a custom page title in wordpress

As a novice , I asked this to lots of paid developers who either said No or quoted exorbitant price or said Not achievable.
But I have achieved few thing on my site by going through all the interactions here. So thought of taking a second opinion.
A custom gateway payment plugin returns to Woocommerce Thank you page , if the transaction is successful but returns to a normal wordpress page -failed-payment, if payment is unsuccessful.
For the unsuccessful payment the return URL is :
mysite.com/failed-payment/?b_name=Jack.
On the failed-payment page, I want to display a personalised Page Title as Sorry, Jack, Your payment was unsuccessful.
Creating a new page template or modifying page template file is not an option. Has to be done via theme function.php.
To write the page title , I have thought of the following to be written in theme function.php :
function assignPageTitle( $title ){
if ( is_page(3062) ) {
$title = sprintf("Sorry, %s, Your payment was unnsuccessful", b_name);
}
return $title;
}
add_filter('wp_title', 'assignPageTitle');
Is it achievable or impossible ? If achievable , how should I approach doing it??
Thanks.
To catch b_name from URL parameter and assign it to php variable is as following:
$b_name = $_GET['b_name'];
Now you can use it in your code e.g:
$title = sprintf("Sorry, %s, Your payment was unnsuccessful", $b_name);
Also I will modify your code a little bit to alter title only when b_name is available (note: I have not tested your WP code so using as it is:)
function assignPageTitle( $title ){
if ( is_page(3062) && isset($_GET['b_name']) ) {
$b_name = $_GET['b_name'];
$title = sprintf("Sorry, %s, Your payment was unnsuccessful", $b_name);
}
return $title;
}
add_filter('wp_title', 'assignPageTitle');
See, how I have added condition isset() for GET parameter, so that it tries to alter page title only when ?b_name= is available in the url.
By below code put into your theme function.php you can modify titles of your page without change / modify any template files:
//modify page title:
function assignPageTitle($title){
$title = $_GET['b_name'];
if ( is_page(3062) ) {
return $title;
}
else{
return $title;
}
}
add_filter('pre_get_document_title', 'assignPageTitle');
//Modify entry title
add_filter( 'the_title', 'suppress_title', 10, 2 );
function suppress_title($title, $id = null){
$b_name = $_GET['title'];
if ( is_page(3062) ) {
$title = sprintf("Sorry, %s, Your payment was unnsuccessful", $b_name);
}
return $title;
}

Change/filter text of link of navigation menu

What I have:
I have a log-in/log-out link in a standard WordPress navigation menu. So far, I've conditionally filtered the URL to either the necessary log-on or log-out urls.
What I need:
I also need to conditionally change the textual value of a link (menu item) in the navigation menu.
My code:
add_filter( 'nav_menu_link_attributes', 'menu_override_b', 10, 3 );
function menu_override_b( $atts, $item, $args ) {
if ( is_user_logged_in() ) {
$url = wp_logout_url();
$newlink = str_replace("http://--loginout--", $url, $atts[href]);
$atts[href] = $newlink;
//None of the following work...
/*
$title ="Logout";
$atts[title] = $title;
$atts[post_excerpt] = $title;
$atts[description] = $title;
$atts[attr_title] = $title;
$atts[post_title] = $title;
$atts[post_content] = $title;
*/
}
else{
$url = "/somewhere/else";
$newlink = str_replace("http://--loginout--", $url, $atts[href]);
$atts[href] = $newlink;
//None of the following work...
/*
$title ="Login";
$atts[title] = $title;
$atts[post_excerpt] = $title;
$atts[description] = $title;
$atts[attr_title] = $title;
$atts[post_title] = $title;
$atts[post_content] = $title;
*/
}
return $atts;
}
List of WordPress menu item attributes
I know it's an old post but in case anyone's looking...
You need to replace $atts[href] with $atts['href'] in your code.
I use this really nice plugin from here
https://wordpress.org/plugins/menu-items-visibility-control/
This allows you to set a call back to conditionally hide menu items, what I would do is have a menu item for when they are logged in and another for when they are logged out, and just swap them.
I'd have to look up the exact code you would need for your callback, but It should go in the theme functions file, then you just run that function on the navigation items, as per the plugins instructions.
It's a bit of a different approach then you are currently doing, but it might be a bit cleaner to keep the navigation items separate and fully functional ( not reliant on external code for their content, only their visibility )
It's also very easy to expand this to other links,
Hope it helps.
--update looking at it, it might have the functionality you need already built in. I use it for product subscriptions from a third party app etc.. but same idea really.

Categories