On my wordpress blog my images link to another page and I would like to remove that link. I'm pretty sure it can't be done using Wordpress' hooks and I've been trying with preg_replace, but to no success.
So Here's an example:
This is a simple link while this is an image link <img src="">
So I only want the images' URL to be removed, while the text one remains.
Any ideas ?
Thank you
You can use regex:
*(<img src="[^"]+" */?>) *
and replace with '\1'.
You can hook the the_content filter and apply a regex with preg_replace to do that.
Here's some info and examples on using the hook: http://codex.wordpress.org/Plugin_API/Filter_Reference/the_content
Related
I want to construct a very basic shortcode solution in a simple non-WordPress website I am maintaining, to allow users to enter content that will be pulled from a database and rendered to screen, allowing them to create a href buttons.
I've seen various classes for this but want to do this in just a line or two of code if possible.
Only they will enter via a shortcode with this format:
[BUTTON link="www.test.com" label="Click here"]
So far I have this which doesn't extract the attributes properly:
$copy=preg_replace('/\[(BUTTON )(.*?)\]/', '$1', $copy);
Can anyone advise?
I assume that you don't actually want to capture the BUTTON, but the link and the label.
Code: (Demo)
echo preg_replace(
'/\[button link="([^"]+)" label="([^"]+)"]/i',
'$2',
$text
);
maybe check how WordPress implemented it?
https://github.com/WordPress/wordpress-develop/blob/6.0.2/src/wp-includes/shortcodes.php
and if the above link doesn't work, check up WordPress core , how they implemented the short code functionality on GitHub.
I am trying to optimize asset loading of a plugin that I do not own. The plugin adds content to pages via shortcodes that may or may not contain conditionals that I am after. I need to somehow get the content fully rendered inside wp_enqueu_scripts and do regex to determine if assets should be loaded. Is this possible?
So far I have tried:
get_the_content() - only shows unrendered names of shortcodes.
the_content filter hook - runs after wp_enqueu_scripts so does not work.
the_content() function - actually echoes the content which is no good for just a check.
The official way to get the rendered content is applying the the_content filter to the content. This way:
$id = 0;
$content = get_the_content($id);
$content = apply_filters('the_content', $content);
All the filters that are registered will run, so also the shortcodes that have already been added. In case these are not added yet, you should revert to using javascript on the frontend to tackle the problem.
I have a wordpress website which is in both english and french, meaning that for every english page, there's its french counterpart with the same content. Until now, when I wanted to add something to it like a slideshow, I'd do two of them and add them on their respective pages but I have a problem with galleries and solo images as their caption and title are used directly in those cases. I don't want to upload each image twice with different properties so I wanted to add a function in php that switches the language for me.
My idea was to put tags in the images caption to have something like that :
[EN] EN description
[FR] FR description
And have the function get this string and work it to remove what has to be removed for the current page. My problem is that I don't really know php and even less the WordPress api of which I struggle to understand how it works, what I get when I call a function or even what it's talking about in the functions description.
So my question is this: Is there a way to get all the images of a page and edit their properties ? If yes, how ?
I tried to search for clues on the internet and found this code that I wanted to use as a base but I'm not sure it's working:
$PageID = get_the_ID();
$Post = get_post( $PageID );
$Content = $Post->post_content;
$Regex = '/src="([^"]*)"/';
preg_match_all( $Regex, $Content, $Matches );
foreach( $Matches as $Image ):
echo '<img src="'.$Image.'" alt="'.$Post->post_title.'" title="'.$Post->post_title.'">';
endforeach;
Maybe I'm missing something but when I try to print $Matches or $Image in the console, all I get is "Array" and it gives me a broken image with my page title on its side if I try to run it.
Thank you for your time.
If you want to change HTML markup for each image (ex. add custom attributes to each <img>), maybe this is a good thread: Change html structure of all img tags in Wordpress # wordpress.stackexchange.com
If you want to change how WP outputs the caption for an image, which is inserted based on a shortcode in the editor, then this WP codex page should help: Plugin API/Filter Reference/img caption shortcode # codex.wordpress.org
There is an example in the second link. You should be able to get the image caption in a variable (see $attr['caption']) - and then you should filter it (ex. remove the [EN] description). But you need to figure how to access the language of the page in that function.
Please tell me, - is there a code I can use to automatically copy the title of a post and rename the filename of that post's featured image according to that title, putting dashes instead of spaces, removing special characters, and converting to lowercase, - basically the usual stuff that WordPress does to post names automatically... And all that at the moment that post is published..?
Thank you in advance!
Alex
Hook into save_post to get the event when a post is saved
add_action( 'save_post', 'update_file_image_name' );
Then you can use the function update_file_image_name
This will pass you the ID of the post in question
function update_file_image_name($post_id){}
Use
wp_get_attachment_url($post_id);
To get the full path to the image. From here, you're just going to use that url by splitting and taking what you need to walk the directory, find the file, sanitize the string as you'd mentioned, and then return out when done.
Is there a way that I can remove links in posts via my functions.php file. Basically I don't want anyone to be able to go outside of the blog posts that are viewed. I have hundreds of posts so I obviously can't go through all of them and remove them manually. Or could I use javascript?
Thanks so much.
Updated: The jQuery below is great. Does anyone know if there is a way I can do it thru php in my functions.php file? If, for whatever ridiculous reason, someone has JS disabled is why I ask.
Thanks!
You could use JavaScript, but you're not going to be able to stop people leaving if they want to.
Something like this may work, although I haven't tested and it was written off-hand:
<script>
$('#content a').each(function() {
$(this).replaceWith($(this).text());
});
</script>
With the jQuery library, this should replace all <a> tags with what was in between them.
So Google should become just Google.
You can strip out the links on the fly using a regular expression -
$post_content = get_the_content();
$post_content = preg_replace( "|<a *href=\"(.*)\">(.*)</a>|", "\\2", $post_content );
echo $post_content
This would need to go in your theme wherever you print the_content. Untested.