I am trying to redirect all post pages, once a post or products are saved. It is redirecting to that page but it is not showing any message. Let me know how to register message for particular pages at admin side.
add_filter('wp_insert_post_data', 'ccl', 99);
function ccl($data) {
if ($data['post_type'] !== 'revision' && $data['post_status'] == 'publish') {
$data['post_status'] = 'draft';
add_filter('redirect_post_location', 'my_redirect_post_location_filter', 99);
}
return $data;
}
function my_redirect_post_location_filter($location) {
remove_filter('redirect_post_location', __FUNCTION__, 99);
$url='.php';
$location = add_query_arg('message', 99, $url);
return $location;
}
add_filter('post_updated_messages', 'my_post_updated_messages_filter');
function my_post_updated_messages_filter($messages) {
$messages['post'][99] = 'Publish not allowed';
return $messages;
}
I am trying to redirect all post pages, once a post or products are saved. It is redirecting to that page but it is not showing any message. Let me know how to register message for particular pages at admin side.
add_filter('wp_insert_post_data', 'ccl', 99);
function ccl($data) {
if ($data['post_type'] !== 'revision' && $data['post_status'] == 'publish') {
$data['post_status'] = 'draft';
add_filter('redirect_post_location', 'my_redirect_post_location_filter', 99);
}
return $data;
}
function my_redirect_post_location_filter($location) {
remove_filter('redirect_post_location', __FUNCTION__, 99);
$url='http://legalpropertieshub/master/wp-admin/edit.php';
$location = add_query_arg('message', 99, $url);
return $location;
}
add_filter('post_updated_messages', 'my_post_updated_messages_filter');
function my_post_updated_messages_filter($messages) {
$messages['post'][99] = 'Publish not allowed';
return $messages;
}
Share
Improve this question
edited Sep 15, 2014 at 17:03
Gabriel
2,24810 gold badges22 silver badges24 bronze badges
asked Sep 15, 2014 at 16:55
Kishan AnemKishan Anem
111 silver badge3 bronze badges
2 Answers
Reset to default 1You could use filter post_updated_messages
:
//Message handling when updating posts
function set_messages($messages) {
global $post, $post_ID;
$post_type = get_post_type( $post_ID );
$obj = get_post_type_object($post_type);
$singular = $obj->labels->singular_name;
$messages[$post_type] = array(
0 => '', // Unused.
1 => sprintf( __($singular.' uppdaterad. <a href="%s">Visa '.strtolower($singular).'</a>'), esc_url( get_permalink($post_ID) ) ),
2 => __('Custom field uppdaterad.'),
3 => __('Custom field borttagen.'),
4 => __($singular.' uppdaterad.'),
5 => isset($_GET['revision']) ? sprintf( __($singular.' återställd till revision från %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => sprintf( __($singular.' publicerad. <a href="%s">Visa '.strtolower($singular).'</a>'), esc_url( get_permalink($post_ID) ) ),
7 => __('Sida sparad.'),
8 => sprintf( __($singular.' skickad. <a target="_blank" href="%s">Förhandsgranska '.strtolower($singular).'</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
9 => sprintf( __($singular.' schemalagd för: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Förhandsgranska '.strtolower($singular).'</a>'), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
10 => sprintf( __($singular.' utkast uppdaterad. <a target="_blank" href="%s">Förhandsgranska '.strtolower($singular).'</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
);
//Special conditions, other messages based on which post-type when updating
if ($obj->name == 'product') {
//Do something
}
return $messages;
}
//Set messages array for posts. Special cases are handled based on post-type
add_filter('post_updated_messages', 'set_messages');
I added this hook to function.php file. "function.php" file is located inside your theme folder, copy this code, paste it inside the file and save.
add_action('save_post','redirect_page');
function redirect_page(){
$type= get_post_type();
switch ($type){
case "post":
$url= admin_url().'edit.php?msg=post';
wp_redirect($url);
exit;
break;
case "product":
$url= admin_url().'edit.php?post_type=product&msg=page';
wp_redirect($url);
exit;
break;
case "page":
$url= admin_url().'edit.php?post_type=page&msg=page';
wp_redirect($url);
exit;
break;
}
}
after that below code to wp-admin/edit.php line 272
if(isset($_GET['msg'])){
$type1=$_GET['msg'];
switch ($type1){
case "post":
echo '<div id="message" class="updated"><p>Post Saved</p></div>';
break;
case "product":
echo '<div id="message" class="updated"><p>Product Saved</p></div>';
break;
case "page":
echo '<div id="message" class="updated"><p>Page Saved</p></div>';
break;
}
}
anyone let me know the better way to do this.