Add custom text to WP Title - php

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.

Related

Only target posts and not pages when using the_content() PHP function with WordPress

I'm having trouble changing the posts display in my WordPress website. So far, the posts can display a title and text content, and I would like to display tags, categories and an image. I added the following code in functions.php and it actually displays the p tags, but around the content, and not in it. Also, it is displayed on all pages of my website, while I just want to add these HTML tags inside the posts.
So,
How can I put the tags inside the_content() and only display the custom HTML in posts?
I hope my question is clear, I starting learning PHP a few days ago, sorry!
Thank you so much in advance for you help!
The code :
// Creating a custom function that appends HTML to the content
function bts_add_html_to_content( $content ) {
$html_segment = '<p>Text to be added.</p>';
$content = $html_segment . $content . $html_segment;
return $content;
}
add_filter( 'the_content', 'bts_add_html_to_content', 99);
Not totally sure what you want for the "in it" part but I can help with conditionally applying your code to posts only... See below:
// Creating a custom function that appends HTML to the content
function bts_add_html_to_content( $content ) {
// if not a post then return the $content as is
if ('post' !== get_post_type()) {
return $content;
}
// must be a post if we get here so lets do something with it
$html_segment = '<p>Text to be added.</p>';
$content = $html_segment . $content . $html_segment;
return $content;
}
add_filter( 'the_content', 'bts_add_html_to_content', 99);

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

WordPress: Add text after (or before) previously posted content based on publish date

I've been searching for a while now with no certain answer. I'm looking to append text to a WordPress site for prior posts that is moving from one domain to a new one and retaining content.
So what I want to do is, add "This article was originally posted at xyz.com." to all posts that were posted before today's date.
Right now this could be done through the database, or a WP functions filter, I'm okay with either option as long as it is long lasting.
Any suggestions on how to go about this would be appreciated?
You can use the_content filter that like this:
add_filter( 'the_content', 'old_wp_content' );
function old_wp_content( $content ) {
if( get_the_date('Y-m-d') < "2017-02-28" ) {
$content = "<p>This article was originally posted at xyz.com.</p>" . $content;
}
return $content;
}
This filter gets fired when you call the_content() of a post. with the_content filter you can adjust the return value of the the_content() function.
I finally got it. That extra "if" before the get_the_date statement and possibly the double quotation marks (swapped for single) wrapping the p tags for the inserted text were the culprits. The following code works:
function old_wp_content( $content ) {
if (get_the_date('Y-m-d') < '2017-02-28' ) {
$content = $content . '<p>This article was originally posted at <a
rel="canonical" href="#">xyz.com</a>.</p>';
}
return $content;
}
add_filter( 'the_content', 'old_wp_content' );
#kevinvhengst, thanks again for your time and patience in helping me figure this out!

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

Wordpress shortcode filtering the_content modifies all posts in a list

I'm writing a plugin and have code like this:
add_shortcode('test','testfunc');
function testfunc(){
add_filter('the_content','mycontent',20);
}
function mycontent($content){
return "<p style='color:red'>extra content</p>";
}
I have used the shortcode '[test]' on one of my posts.
The problem is when a list of posts is shown - for example when using a category view - the content is changed for all of the posts displayed - not just the one with the shortcode in it.
Any ideas how I can change it so it only filters the code
Hi heres a solution is doesn't use a shortcode hook but it searches for it in the content.
function content_custom_shortcode($content) {
// Search for shortcode
$shortcode = 'test';
preg_match('/\['.$shortcode.'\]/s', $content, $matches);
// If custom shortcode exists return custom content else return content
if (in_array('['.$shortcode.']', $matches)) return "<p style='color:red'>extra content</p>";
else return $content;
}
add_filter('the_content','content_custom_shortcode',20);
Any time you display a list of posts and one of them contains the shortcode you've created it will change all following posts that are displayed because you've added it to the_content, which is executed on every iteration of The Loop. Remove the filter and it should work as expected.
This will wrap the content that you surround with [test] and [/test] in the <p> tags:
add_shortcode('test','testfunc');
function testfunc($atts, $content=NULL){
return "<p style='color:red'>{$content}</p>";
}
If you would like to replace all of the content in the post with replacement text change the variable {$content} in the example above to whatever text you would like. You still need to wrap the post content in [test] and [/test].
There is an example in the WordPress Codex entry on shortcodes that is very similar to this.

Categories