How to replace text & URLs in shortcode output - php

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! :(

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;
}

Add custom text to WP Title

I am able to add a custom text to my title of a specified post id but it does not apply to my meta title and my rss feed title. It is seen only by the user on the current page.
I tried using is_feed() but with no success...
This is what I'v done so far:
add_filter('the_title', 'add_my_title', 10, 2);
function add_my_title($title) {
$my_custom_text = 'Sponsored: ';
if(is_single( '99' )) { $title = $my_custom_text.$title; }
return $title; }
My title should look like:
"Sponsored: Are you prepared for the Black Friday?"
and must be the same/applied everywhere - html, feed, facebook/twitter opengraph etc.
Thank you in advance.
The easiest way is to add text to the headers from the settings of your seo-plugin.

Replace Shortcodes with HTML in Wordpress

I'm working on a WordPress site that has been using a plugin to grab amazon product images using a shortcode.
You simply insert the asin of a book into a shortcode, like this:
[amazon template=image&asin=B00FYY53B8]
When the post loads, the shortcode is converted into the actual image HTML using the URL from amazon.... so the example above would return
http://ecx.images-amazon.com/images/I/71kIifYTccL._SL1500_.jpg
I'm using another custom plugin to build out the content for posts, and so far am just using this shortcode as part of the post content. I would like to do some other things, such as set the featured image, etc. and need that URL.
I've tried
echo do_shortcode( '[amazon template=image&asin=B00FYY53B8]' );
But it doesn't return anything. Is there a way to "execute" shortcodes in a plugin and save the output?
Ultimately, I would also like to scale this functionality so I can replace other/old shortcodes with their HTML output, and save it back to the post, essentially eliminating the use of some shortcodes in posts.
I have a demo for add a short code in wordpress. I hope it help.
add_action('plugins_loaded','MOD_load');
function MOD_load() {
short_enable_form();
}
function short_enable_form(){
if(!function_exists('add_shortcode')) {
return;
}
add_shortcode('form_mod' , array(&$this, 'out_put_form'));
}
public function out_put_form(){
return 'HTML TEXT';
}
You can include this code in functions.php. And call this function in content-singular.php or the similar file in your theme.
function updatesc_database( $post ) {
if ( empty($post) ) global $post;
if ( empty($post) || ! isset($post->post_content) ) return false;
$content = $post->post_content;
$shortc1="amazon template=image&asin";
$shortc2="B00FYY53B8]"; //insert the number variable here
$shortwh=$shortc1.$shortc2;
$replace = do_shortcode($shortwh);
$content = str_replace( $shortwh, $replace, $content );
return $content;
}

Remove everything from output except between <p> tags

I have a Wordpress site that outputs content from individual blog posts with the_content()
Blog posts all consist of two things, a small gallery and some text:
<div class="gallery"><img></img>Blah Blah</div>
<p>Text</p>
<p>Text</p>
I'd like to split the gallery and the text and output the gallery in a div on the left and the text in a div on the right like this:
<div id="left">GALLERY CONTENT</div>
<div id="right">TEXT CONNTENT</div>
I have tried to do this with strip_tags(the_content(), '<p>') but this does not - it continues to output everything including the gallery.
What is the correct way to do this?
It is not really clear to me what you really want to do , and on to of it , you included some (very little ) source code from output, but to get a real shot at answering you need to include the relative code from the template file.
( And +1 for understanding alone that you should NOT TOUCH YOUR CORE FILES )
Anyhow, I suspect that you only want to disable the auto P generated by wordrpess , so try
remove_filter('the_content', 'wpautop');
(add to functions.php in theme.)
alternatively , you could use
add_filter('use_default_gallery_style', '__return_false');
Which will just "reset" the gallery styling .
or even filter your own gallery styles , which allows you to target them better.
add_filter( 'gallery_style', 'my_own_gallery_style', 99 );
function my_own_gallery_style() {
return "<div class='gallery'>"; // put your own
}
If it does not produce the right output for you, please include more specifics and / or more code .
There are of course more advanced ways to handle this , but Without more info it is difficult to target .
For example you can create your own gallery style by removing the original shortcode function, and then adding your own, but those are a bit more advanced techniques.
// deactivate WordPress function
remove_shortcode('gallery', 'gallery_shortcode');
// activate your own own function
add_shortcode('gallery', 'my_own_gallery_shortcode');
// the own renamed function
function my_own_gallery_shortcode($attr) {
...
}
Now on the other hand , If you want to "catch" some parts of 'the_content' and display it in a loop in a different manner , you can always use a different technique, like described HERE on another answer .
You are using the_content which displays the content instead of returning it.
Change your code to
strip_tags(get_the_content(), '<p>')
I had this exact same problem a while ago. Here's what I did (in single.php, which is where I had the problem):
if ( get_post_format() == 'gallery' ) :
$content = get_the_content();
$gallery_regex = '/\[gallery.*]/s'; //identify the [gallery] tags within the content
//get gallery code
$gallery = preg_match($gallery_regex, $content, $matches);
$gallery = $matches[0];
//remove gallery from content
add_filter('the_content', function($content){
$gallery_regex = '/\[gallery.*]\s*/s';
return preg_replace($gallery_regex, ' ', $content);
});
endif;
Basically, I used regex to remove the gallery tags from the content.
$gallery still contains the shortcode. We can't just randomly display it, or it'll actually show the shortcode. We need to execute it, which will show the output:
if ( get_post_format() == 'gallery' ) {
echo '<div id="left">'. do_shortcode($gallery) .'</div>';
}
Your content no longer contains the gallery, so you can do this:
<div id="right"><?php the_content(); ?></div>

Changing wp_title from inside my Wordpress Plugin

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);

Categories