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.
Related
Wordpress | Advanced Custom Fields plugin
I created a new field in the editor where my colleagues can fill in a URL relevant to that specific page. This URL will be used in the button we load on every single job listing page. This button is created within the functions.php file:
function my_editor_content( $content ) {
$content = "<h3>Title</h3><p>text</P>
<a href='[acf field='afas_url']' target='_blank' rel='noopener'><button>Solliciteer Nu</button></a>";
The above is cut off at the 'afas_url' part. So, I think it has something to do with the quotations marks and probably not even hard to fix, but as I'm just starting to figure things out I couldn't find the answer myself yet.
I hope it's a clear enough explanation :)
(And maybe it's not best practice to use functions.php but it was already like this)
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.
I have been searching and found a lot of various answers, however, I have not found a definitive answer.
I need to run a function right after a post is done saving to the database. This includes every aspect of the post including post metas. I have tried to hook into save_post but that seems to run my function before post metas are saved. I have also tried post_updated and updated_postmeta, but my function doesn't seem to run on either of them.
Another thing to note, I need to have access to the post ID inside my function.
Edit, My plugin uses the Advanced Custom Fields plugin and the function I have coded uses update_field to either create new post metas or update existing one based on some stuff. This code works. When I run the function at the post_updated hook the function seems to run but nothing happens. If I add die() to the end of my function my code works, but die kills the page and all I am left with is a blank white page at the url wp-admin/post.php. So adding die allows my function to work and I am not sure why it would not work without die.
I would comment your post, but I cannot because I dont have 50 rep.
Do you mean the_post?
https://codex.wordpress.org/Plugin_API/Action_Reference/the_post
function my_the_post_action( $post_object ) {
// modify post object here
}
add_action( 'the_post', 'my_the_post_action' );
it ought to have the post Id
https://developer.wordpress.org/reference/hooks/the_post/
Okay I found how to make publish_post work.
For custom post type you need to replace the "post" by the post type slug.
Example with the custom post type "Recipe" with "recipe" slug.
add_action('publish_recipe', 'test_publish_post', 10, 2);
function test_publish_post($post_id, $post){
wp_die($post_id);
}
Don't forget to wp_die() or die(); else you will be redirected and you won't see your var_dump();
I was able to
fix my issue. It turns out that save_post does seem to run after post metas are saved. My problem actually came from something else inside my code that I was able to fix by changing how I handled that part of my script.
I'm currently just experimenting with wordpress PHP functions. There is a thing I don't understand. I made a plugin which gets photos in URL format, then puts them in a shortcode which just pastes the individual photo to Wordpress posts. How should I make a shortcode which would just set the thumbnail for the specific post it was posted to?
bit of source code:
function fone(){
global $images;
return ($images[0]);
}
add_shortcode( 'one', 'fone');
tried searching about set_post_thumbnail(), but it needs a thumbnail ID which I don't have. Any help?
You should check this function:
media_sideload_image($url, $post_id=0, $desc='', $return='html');
i am newbie to wordpress theme development...
i know to create theme options in wordpress themes but now i want to use WYSIWYG Editor of wordpress, the wp_editor(), i have read some tuts on this but i can't make it...
here is my code:
add_settings_field('tinytxt', 'WYSIWYG: ', array($this, 'tinytxt'), 'oditer_theme_options', 'jd_theme_options_main_section');
public function tinytxt() {
wp_editor("{$this->options['tinytxt']}", 'tinytxtboom');
}
And how to retrieve the saved content from the database, i know to use get_option()...
thanks in advance...
You need to give your WordPress editor a name for the input, so that it can save the value.
wp_editor( $this->options['tinytxt'], 'tinytxtboom', array(
'textarea_name' => 'jd_theme_options[tinytxt]'
) );
This will give the hidden textarea wp_editor uses a name for the form submission. jd_theme_options matches the second argument to register_setting, and tinytxt is the option key you want to save the value under.
I think that's all you need. Comment if you have trouble. I'll be back tomorrow to check.
You could also try getting it to work with just a simple textarea, then try to get the wp_editor working.