Hi I'm type to make a plugin which shows a page instead of sending a 404 page when on a page that doesn't exist
I know I can edit to 404.php file to do this but i need to fit this in a plugin.
If I remove the if statement then its works on all pages which isn't what I want.
So i put the if(is_404()) statement in but it doesn't seem to work at all now...
function da_404_redirect()
{
if(is_404())
{
query_posts('page_id=2');
status_header( "200" );
}
}
add_action('init','da_404_redirect');
Your action hook may be too soon in the wordpress cycle to know if it is a 404. http://codex.wordpress.org/Plugin_API/Action_Reference/init. Try using a later action hook.
As Nick says above you might be hooking into a late-firing event. In my experience, template_redirect is the best option.
To redirect, don't hook on init, especially if you use conditional tag like is_404(). Any conditional tag will still return false in init.
USE: template_redirect instead or pre_get_post if you need to change something on the WP_Query
Related
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.
Since I don't want to use another plugin to do simple redirect tasks, I decided to use the following code.
wp_redirect( "http://www.example.com/contact-us", 301 );
so here is my question.
Let's say I have a page called "https://www.example.com/contact-us-3/" that needs to redirect to "https://www.example.com/contact-us/".
Or I should say I really want the redirect to happen regardless of http or https, www or non-www.
I am want "/contact-us-3/" to redirect to "/contact-us/" page.
Does that mean I have to put the following code inside the wordpress contents? Where do I exactly put the code? function.php in the child theme? I do I specify the page that needs to be redirected? or I do have to create a page "/contact-us-3/" and put the code in the page?
Also, do I have to put the fully qualified domain name url?
You may want to put your redirect code into a callback function that is tied to the template_redirect hook. Put code similar to the following in the functions.php file of your theme. The function named "some_condition" (which you write) will determine if the page should be redirected or not.
add_action( 'template_redirect', 'my_callback' );
function my_callback() {
if ( some_condition() ) {
wp_redirect( "http://www.example.com/contact-us", 301 );
exit();
}
}
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
I'm trying to allow my page to receive query variable but to rewrite it to nice permalink. So I wanted this
example.com/wordpress-page/random
So I don't want random to be a subpage or something like that, since it's coming from outside service. In order to do this, I've added this code to my functions.php
function add_my_var($public_query_vars) {
$public_query_vars[] = 'my_var';
return $public_query_vars;
}
add_filter('query_vars', 'add_my_var');
function do_rewrite() {
add_rewrite_rule('wordpress-page/([^/]+)/?$', 'index.php?name=wordpress-page&my_var=$matches[1]','top');
}
add_action('init', 'do_rewrite');
When I go to example.com/wordpress-page I get my page, but when I go to example.com/wordpress-page/random I get 404 page.
I have flushed rewrite rules by saving permalinks in wp-admin panel.
Where did I go wrong?
I found I needed to call the flush_rewrite_rules() function. That was done in addition to the query_vars action hook and add_rewrite_rule() function you already demonstrated above.
In my case I chose to put it in my theme's functions.php using the switch_theme action hook and deactivated/reactivated my theme. But you can probably put it elsewhere as long as it get's called once at some point (so probably not 'init')
add_action('switch_theme', 'mytheme_setup_options');
function mytheme_setup_options () {
flush_rewrite_rules();
}
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