wordpress - action hook 'pre publish post'? - php

need to change post content before it is published. I could overwrite the native publish function but don't want to do this.
What i'd like to do is something like:
add_action('before_publish_post','my_func');
function my_func($content){
$content = "new content";
return $content;
}
I have had a look at the 'publish_post' hook, but this only allows me to change the post content afer it has been published, not before.
any help would be appreciated,
cheers

It should be the filter wp_insert_post_data

If I was really cool I'd wait until the Anniversary date of this post & do it then, but...
It's also possible to use:
add_action('pre_post_update','alter_post_contents');
I'm coding an admin interface right now that will make use of this so I'll report on whether it was effective. I saw one other post here related to the use of pre_post_update, but it claims to make use of post id as the arg- and their post.php page seems to bear this out- whereas the codex states that content is the arg and that suits your purposes dead on....
Seems testing will show this one way or another.

Use save_post hook.
You can read more here: http://codex.wordpress.org/Plugin_API/Action_Reference

Related

Wordpress hooks after a post is saved

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.

Read POST data in Wordpress page

I am trying to write a plugin for wordpress. I have a problem i can't solve.
Inside plugin i habe added a shortcode that show a form.
function showForm() {
echo '<form method="post" action="www.example.com/myDestinationPage">';
[...]
}
add_shortcode( 'ShowFormSC' , 'showForm' );
After that, in a page, i added the shortcode and it works perfectly. ;-)
Now the problem: how can i read POST data in myDestinationPage (another wordpress page)?
In Php would be very simple ... but in wordpress I do not know how to do.
Second problem: myDestinationPage must be a real wordpress page with another shortcode inside, or can be defined as a "virtual" page inside my plugin?
Thank you for your help!
Best Regards,
Simone
www.example.com/myDestinationPage needs to be edited to recieve the post data, just as in any other php script, wordpress or not. If 'myDestinationPage' resolves to dynamic wordpress content, then you are in muddy waters.
Let's say, myDestinationPage is a wordpress Post. That "page" doesn't exist as a file, it comes directly from the database.
You could write a shortcode which handles this though:
add_shortcode('post_parser', 'postParser');
. . .
function postParser() {
filter_input(INPUT_POST, 'my_post_value');
//do something
}
Then, you just add the '[post_parser]' shortcode to the myDestinatioPage Post. (You mentioned it's a wordpress page, but page & post are both WP_Post objects.)
Another option is to put your post processing code in the post.php (or whichever template myDestinationPage is).
1st answer: you can directly use $_POST in wordpress like in php.
2nd answer: Yes you can. If you want to use pages in your plugin, use plugins_url() to generate the path for form action.
https://codex.wordpress.org/Function_Reference/plugins_url

WP Jetpack publicize insert default text(s)

How can I place a default text (hashtag) in the Custom Message?
The textarea is (located in line 643) under jetpack/modules/publicize/ui.php
I tried to put the text in front of $title in various ways, like:
<?php echo "#myhashtag $title"; ?>
or
<?php echo '#myhashtag '.$title; ?>
but it just echoes the text, not the $title.
Any ideas will be greatly appreciated.
You can use the approach of this Wordpress plugin i made (Publicize With Hashtags), which does exactly that. It basically use and action trigger bound to the 'save_post' native event.
If you want to develop your own one you can have a look at my Source Code on the project's GitHub page or in this installation & usage guide I wrote about it.
You can add a filter, like so, to your theme's functions.php or a site-specific plugin:
add_filter( 'wpas_default_prefix', 'add_default_publicize_hashtag_prefix', 10, 4 );
function add_default_publicize_hashtag_prefix() {
$default_tags = '#yourhastaghere ';
return $default_tags;
}
This will add your default hashtag before your title without you needing to hack the WordPress core.
jetpack/modules/publicize/ui.php itself states in its comments:
/**
* Only user facing pieces of Publicize are found here.
*/
You added your hashtag in the textarea which allows admins to enter a custom message (click edit and it will slide down with your hashtag).
As #Yazmin mentioned, the best way to permanently edit the message is using a filter. The filters available are wpas_default_prefix, wpas_default_message, and wpas_default_suffix.
Personally, I had no success using these filters and I'm interested in a working solution to this issue myself.

Wordpress Custom post type and permalink

I have these two statements in my init
add_rewrite_tag('%cirrus_url%','([^&]+)');
add_rewrite_rule('^listings/([^/]*)/([^/]*)/?','index.php?pagename=$matches[1]&cirrus_url=$matches[2]','top');
Using the parse_request filter, I can see that my rule is being matched and used.
However, Wordpress is forwarding the page, so all my /vars/at/the/end are lost when the page renders.
When I output something from the query_var filter, I get a Headers already sent message, so I know that wordpress is trying to forward the page.
http://1parkplace.mysharedvision.com/dev/listings/travis-heights-test/
If you put something at the end like:
http://1parkplace.mysharedvision.com/dev/listings/travis-heights-test/what-is-this/
You'll see what I mean.
Any ideas?
Figured it out
add_rewrite_tag('%cirrus_url%','([^&]+)');
add_rewrite_rule('listings/([^/]*)(/[^/]+)?$/?','index.php?cirrus_posts=$matches[1]&cirrus_url=$matches[2]','top');
My custom post type was cirrus_posts so I just looked at wordpress' <?php var_dump($wp_rewrite->wp_rewrite_rules()); ?>
To see what it was doing and that let me to the right thing

Changing CCK Title for Form

By default a CCK form creation has a title of the form
Create [Your Content Type Name Here]
I want to change mine to
Register for Such and Such
It was suggested that I could use string-override, but I can't find the string to replace. I've also tried writing code to form_alter, but can't seem to figure out how to get the "title" to change.
Ideas?
There are two possibilities, either you can use the theming laying and set $title variable used in the page templage. You can do this with a preprocess function like lazy suggests.
The other options which I prefer would be to use the drupal_set_title(), this would need to go in a module. I haven't tried this, but I would think that you could use this in your hook_form_alter() implementation. That way you could control which titles get changed pretty easily.
Hook form_alter doesn't affect the title, but you can use a preprocess function:
Try this code to start:
function MYMODULENAME_preprocess(&$variables) {
$variables['title'] = 'test title';
}

Categories