i have to get the content and find the url from the content and replace it with the another default url.whatever content is there i have to find the href and replace it with the default url.
The shortcode changes every url to a new random cat using this simple API: http://random.cat/meow
Example:
[demo_shortcode]<img alt="A boring image of something other than a cat." src="http://i.imgur.com/BTNIDBR.gif">[/demo_shortcode]
Outputs:
<img alt="A boring image of something other than a cat." src="http://random.cat/i/zUhVw.jpg">
for try i am trying this to get
function demo_function( $atts, $content = ""){
$pattern = '(<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>)';
$varr=file_get_contents('http://random.cat/meow');
$content = preg_replace($pattern,$varr,$content);
return $content;
}
add_shortcode( 'demo_shortcode', 'demo_function' );
[demo_shortcode][/demo_shortcode]
Output : <a href=”http://random.cat/i/QRp74.jpg“></a>
this replaces the href to the required url but also replaces the image.
thanx in advance.
Related
I am trying to use the content to signify which template_part code to load on my page. For example, if the content == "volume" I want to load get_template_part('lib/volume');
<?php
$content = get_the_content();
$content = strip_tags($content);
$content = html_entity_decode($content);
$content = filter_var($content, FILTER_SANITIZE_STRING);
get_template_part('lib/'.$content);
?>
This is the code in my template, however it doesn't load the desired template part.
You can strip all html tags with using wp_strip_all_tags() to get only the content. strip_tags() won't help you get rid of comments and wordpress gives us the great function to also delete the script and style.
https://developer.wordpress.org/reference/functions/wp_strip_all_tags/
So you can use one line of code to get a clean content:
$content = wp_strip_all_tags( get_the_content() );
Maybe this is the reason. Have you tried so display the value of $content to make sure you have the string you want to have? If it outputs the string the right way, there might be a problem with the get_template_part() function.
https://developer.wordpress.org/reference/functions/get_template_part/
You can try putting the string together, before using in the function:
$content = wp_strip_all_tags( get_the_content() );
$content = "lib/".$content;
get_template_part( $content );
If this is not working, check the name of the folder lib (has to be subfolder in the root of your theme folder) and the name of your php files. It could also be possible that there is something wrong with the template part file and that is the reason it is not loaded. Next check the page template inside of which you are trying to get the template part.
If nothing helps you out, it could be an alternative using include() instead of get_template_part().
I have a little problem, how correct can i find img src string who ends with dealer.jpg and remove only this string from my content? for example:
<?php
$content = '<b>this is a content</b><img src=http://adress.com/as5.jpg><br> this is a content <img src=http://www.another-adress.com/dealer.jpg>';
$inf = explode("/dealer.jpg", $content);
$string = str_replace("<img src=\"$inf[0]/dealer.jpg\">", "", $content);
?>
I use this because I dont know image link, link always is another, but I know this link ends with dealer.jpg , and this script is not working... so can some one help me how correct do this ? normaly I whant to remove ads from page that i scraped. Thank you !
If i understood correctly you are trying to remove the img tag that ends with "dealer.jpg" (no matter the domain), right? try this:
$content = '<b>this is a content</b><img src=http://adress.com/as5.jpg><br> this is a content <img src=http://www.another-adress.com/dealer.jpg>';
$content = preg_replace('/<img src=[A-z0-9-_":\.\/]+\/dealer\.jpg>/', '', $content);
var_dump($content);
Edit
This second example will match the img tag even if it has another attributes such as alt, width, etc (but again, must end with "dealer.jpg")
$content = '<b>this is a content</b><img src="http://adress.com/as5.jpg"><br> this is a content <img alt="dealer-image" width="120" height="40" src="http://www.another-adress.com/dealer.jpg">';
$content = preg_replace('/<img[A-z0-9-_:="\.\/ ]+src="[A-z0-9-_:\.\/]+\/dealer\.jpg">/', '', $content);
var_dump($content);
Obs: I changed the initial $content because i've noticed it was missing the double quotation for src attribute. Not sure if was a typo or your string really looks like this.
Edit 2
Here is a example using DOM (a guess that is the best aproach here since the order of attributes could change):
$content = '<b>this is a content</b><img src="http://adress.com/as5.jpg"><br> this is a content <img alt="dealer-image" width="120" height="40" src="http://www.another-adress.com/dealer.jpg">';
// creates a DOMDocument based on your string, and wraps it in a div
$dom = new DOMDocument();
$dom->loadHTML("<div>{$content}</div>", LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED);
// get all img tags
$imgs = $dom->getElementsByTagName('img');
foreach ($imgs as $img) { // if they have that src, remove it from $dom
if (strpos($img->getAttribute('src'), 'dealer.jpg')) {
$img->parentNode->removeChild($img);
};
}
// get all the content of my first div, and print it
$newContent = $dom->getElementsByTagName('div')->item(0);
foreach ($newContent->childNodes as $childNode) {
var_dump($dom->saveHTML($childNode));
}
I'm guessing there is a simple solution to this, but I can't seem to get my phrasing right when searching for it, so I'll post it here.
I have some links that look like this in Wordpress:
<a target="_blank" href="<?php echo get_post_meta($post->ID, $prefix.'hjemmeside', true); ?>"><?php echo get_post_meta($post->ID, $prefix.'hjemmeside', true); ?></a>
Just regular links that I echo in my single template to create user homepage/facebook etc. The problem is when you click it, the link will just adds itself to the end of the URL:
Example:
wordpress.com/single
when clicking the link:
wordpress.com/single/www.homepagelink.com
Thanks for any help :)
My guess is that wordpress isn't adding anything. If you have the URL without preceding http:// in the custom field it is shown that way by the browser. If you check the generated source code with your browser you will find the code like this:
<a target="_blank" href="www.homepagelink.com">www.homepagelink.com</a>
Without http:// or other valid URL schema this is interpreted by the browser as a relative link and handled as such.
You can either add the http:// in the field value or you place a wrapper function in the functions.php of your theme to make sure it is always interpreted as URL regardless what was put in the field.
function my_field_link($id, $field) {
$value = get_post_meta($id, $field, true);
if (substr($value, 0, 7) == "http://") return $value;
return "http://" . $value;
}
Then you can call this function like this:
<a target="_blank" href="<?php echo my_field_link($post->ID, prefix.'hjemmeside'); ?>"><?php echo my_field_link($post->ID, prefix.'hjemmeside'); ?></a>
Now the link will always start with http://.
Note: If you expect to have other URL schemas in use (https, ftp, scp, etc.) you should adapt the function accordingly.
I have a child theme in wordpress that is based on twentyten.
Some of my authors have hardcoded URLs in their post titles and I want to remove those URLs.
I put the following code in my functions.php file in the child theme, but it has no effect on the display of the post title:
add_filter( ‘the_title’, ‘ib_strip_tags_from_titles’ );
function ib_strip_tags_from_titles( $title ) {
$title = strip_tags( $title );
return $title;
}
Any suggestions?
strip_tags() only removes HTML tags - in your case it will change the title from
Some Text LINK Other Text
to Some Text LINK Other Text
If I understand you correctly, this is what you want:
function ib_remove_links_from_titles($title) {
$title = preg_replace('/<a([^<]*)">([^<]*)<\/a>/', '', $title);
return $title;
}
add_filter( 'the_title', 'ib_remove_links_from_titles' );
going with the above example it will output Some Text Other Text
Note that given that you tried to accomplish the task with strip_tags(), I am assuming the "harcoded URLs", as you described them, are enclosed in <a [...] ></a> tags. If that's not the case you would need a regular expression that matches URLs. That is much more tricky, depending on whether the URLs your authors use are internationalized / have different domains, are not all just http:// prefaced and so on.
I vouch for the above to work if they are enclosed in tags, if not, this regex will catch most URLs, but comes without my guarantee to work in every case:
(([A-Za-z]{3,9})://)?([-;:&=\+\$,\w]+#{1})?(([-A-Za-z0-9]+\.)+[A-Za-z]{2,3})(:\d+)?((/[-\+~%/\.\w]+)?/?([&?][-\+=&;%#\.\w]+)?(#[\w]+)?)?
You'd have put that between the '/ and /' in the above function.
I'm creating a new WP theme and I would like to allow the user to insert a divider in between paragraphs or images he/she is entering, for a post/page.
I want the output to be something like:
<div class="divider"></div>
But I don't want the user to have to enter HTML in the WYSIWYG editor. Is it possible to ask them to enter something like:
<-- break -->
and then translate that to the div markup on display?
Thanks.
Build a function in your theme's functions.php file like this:
function add_div( $content ) {
$content = str_replace( '<!-- break -->', '<div class="divider"></div>', $content );
return $content;
}
then add the following to the theme:
add_filter( "the_content", "add_div" );
The function uses PHP's string replace function to find the text you want your users to input and replace it with the text you want to render, the add_filter() function uses Wordpress's content filter to apply your function to the content of each post after it is read from the database, but before it is rendered to the browser.
This will work in PHP4 and up, which is still the official level of support for Wordpress.