Redirect with notice after form submission - wordpress

I create a sub menu page and there is a form on it. The data is processed by admin-post.php.
I use wp_safe_redirect to redirect back to the previous page after the form is submitted. Now I'm trying the admin_notices hook to display a message to inform user by showing a message after the redirect. But the notice won't show, what am I doing wrong?
add_action( 'admin_post_hidden_value_of_form', 'setting_response' );
function setting_updated(){
?>
<div class="notice notice-success is-dismissible">
<p>Saved successfully!</p>
</div>
<?php
}
function setting_response() {
wp_safe_redirect( admin_url( 'admin.php?page=plugin-setting-url' ) );
exit;
add_action( 'admin_notices', 'setting_updated' );
}

Related

How do I add a wordpress notice message after page redirect?

The following example will redirect all non-authenticated users to a custom 'signup' page when trying to visit the 'goodies' page.
however, I need to display a notification message on the signup page after redirect, What is the best way to do this?
function my_page_template_redirect() {
if ( is_page( 'goodies' ) && ! is_user_logged_in() ) {
wp_redirect( home_url( '/signup/' ) );
die;
}
}
add_action( 'template_redirect', 'my_page_template_redirect' );
here is an example of the code that displays the message
add_action( 'woocommerce_init', 'pt_custom_notice' );
function pt_custom_notice() {
wc_add_notice( 'This is a Error notice', 'error' );
}

How to redirect on particular page in wordpress?

<?php
if($result_array['ACK']== 'Success')
{
wp_redirect( get_page_by_title( 'thank-you' ) );
}else{
//redirect like php
header("Location : http://localhost/mysite/faq");
} ?>
My query is working but I have no idea that how I simply redirect in my page and display any my page content data in wordpress. So please help me.
You should pass the actual title into that function, not the slug:
wp_redirect( get_page_by_title( 'Thank You' ) );

redirect user if not logged into wordpress

I need to redirect users not logged in to any page I choose. Not the login page. I tried placing this code in the header, but it is an endless loop. Anybody know what code I need? Thanks
<?php
if (!is_user_logged_in()) {
wp_redirect( 'http://skeeterz71.com/cyber/about/');
exit;
} ?>
You can do it by adding some code in functions.php file
function user_redirect() {
// Current Page
global $currentpage;
// Check to see if user in not logged in and not on the login page
if(!is_user_logged_in() && $currentpage!= 'wp-login.php')
// If user is, Redirect to Login form.
wp_redirect( 'http://skeeterz71.com/cyber/about/');
}
// add the block of code above to the WordPress template
add_action( 'wp', 'user_redirect' );
If you have the same redirect code on about us page, you can put a check like below
<?php
if (!is_user_logged_in()) {
if(!is_page('about'))
wp_redirect( 'http://skeeterz71.com/cyber/about/');
exit;
} ?>
or, you can check for page template instead of is_page()
if(!is_page_template('page-about.php'))
wp_redirect( 'http://skeeterz71.com/cyber/about/');
exit;

Virtual Page Within Theme Template

I am creating a plugin that needs a virtual page in order to dynamically create a "Review Order" page (Orders are a custom post type I created). I was able to create a virtual page using the following code:
// Set up the rewrite rules for the order page
add_action( 'init', 'action_init_redirect' );
function action_init_redirect() {
add_rewrite_rule( 'orders/?', 'index.php?orders=new', 'top' );
}
add_filter( 'query_vars', 'filter_query_vars' );
function filter_query_vars( $query_vars ) {
$query_vars[] = 'orders';
return $query_vars;
}
add_action( 'parse_request', 'action_parse_request');
function action_parse_request( &$wp ) {
if ( array_key_exists( 'orders', $wp->query_vars ) ) {
//Beginning of page code
echo "hello";
exit;
}
}
The problem is that this creates a page with a blank template, that is, the above code creates a blank page with the text hello. I would like for the virtual page to be within the theme of the site and displayed like a regular page within the WordPress framework. How to accomplish this?
A solution is to add a template page inside parse_request:
function action_parse_request( $wp ) {
if ( array_key_exists( 'virtual', $wp->query_vars ) ) {
get_header(); ?>
<div id="primary">
<div id="content" role="main">
Hello, world!
</div>
</div>
<?php get_footer();
exit;
}
}
try using the following:
define('WP_USE_THEMES', true);
require('/ABSOLUTE_PATH_HERE/wp-blog-header.php');
require('/ABSOLUTE_PATH_HERE/wp-load.php');
you will need to add this to the top of your page.

Wordpress admin menu

I tried hundrad of times to load a custom page when top level menu items are clicked.
add_menu_page('Hotels', 'Hotels', 'administrator', 8, 'main_menu_foo');
Where should I put my "view-hotels.php" file?
I want to built a menu as
[Hotels]
[Hotels] //points to view-hotel.php
[Add new...] // points to add-hotel.php
Add menu page needs a call back function to render the page. You can wrap your page in a function and add with an include or you could include the page using your callback function. Also 'administrator' is not a capability. Heres an example:
add_action( 'admin_menu', 'add_admin_menus' )
function add_admin_menus() {
add_menu_page( 'Hotels', 'Hotels', 'manage_options', call_back_function );
}
call_back_function() { ?>
<div class="wrap">
<?php wp_nonce_field( 'hotels' );
hotels_page_function(); ?>
</wrap>
<?php }
include_once( '/view-hotels.php' );

Resources