WordPress quick access link for protected page - wordpress

I'm trying to create quick access link for protected page. For example if the person went to http://example.com/post/?password=PASSWORD they would be straight in without being asked for the password.
So far I've added filter for the 'the_content' which checks if the post is protected, and it compares the value from url with post password.
This part works fine, but how do I return the content after that? Is there any way I can simulate the password form submission, or get content somehow?
function render_content_or_not_to_render( $content ) {
global $post;
if ( post_password_required() ) {
if ( $_GET['password'] == $post->post_password ) {
return $content; // this way it just returns the password form
} else {
return get_the_password_form();
}
}
}
add_filter( 'the_content', 'render_content_or_not_to_render' );

Added filter for the 'post_password_required' gets the job done. Not sure about security, please consider this as rookie solution.
function modof_post_password_required( $post = null ){
global $post;
if ( $_GET['password'] == $post->post_password ) {
return false;
} else {
return true;
}
}
add_filter( 'post_password_required', 'modof_post_password_required', 0 );

Related

WordPress snippet - post password protection check

Hello i want to run a content modification snippet but only on password protected posts.
function func($content) {
if( $post->password_protected ){
$content = [whatever]
}
return $content;
}
add_filter('the_content', 'func');
how can i do that password_protected check?
I figured it out
if( !empty($GLOBALS['post']->post_password) ){..}

Custom Redirection of A Page On User Role

I am working with WooCommerce and in the website, there are three specific users. i) Admin ii) Vendor iii) Customer. So what I want is to redirect to a specific page, when the user role is Vendor. So I am hoping there would be a way to do it, something like the below:
function vendor_dashboard_redirect() {
if (condition) {
redirect("To The Default WordPress Dashboard");
}
}
add_action('template_redirect', 'vendor_dashboard_redirect');
I am expecting there would be an appropriate way to do it and stuck with it for a while.
This shoud work. Change the $vendor_role variable to your custom role identifier:
function vendor_dashboard_redirect() {
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
$roles = $user->roles;
$vendor_role = 'vendor';
if ( in_array( $vendor_role, $roles ) === true ) {
wp_redirect( admin_url('/') );
exit;
}
}
}
add_action('template_redirect', 'vendor_dashboard_redirect');

Wordpress Plugin Custom Post Type Archive Template results in other pages not displaying

This is the code I am using to call my archive template, now the knowledgebase section works, the forums work, but all other pages throughout the website are now blank.
function kb_archive_template_function($arhive_template){
if(is_post_type_archive('knowledgebase')){
$theme_files = array('/templates/archive-knowledgebase.php');
$exists_in_theme = locate_template($theme_files, false);
if($exists_in_theme == ''){
return plugin_dir_path(__FILE__) . '/templates/archive-knowledgebase.php';
}
return $archive_template;
}
}
You can use this code ,instead of your code
function get_custom_post_type_template( $archive_template ) {
global $post;
if ( is_post_type_archive ( 'knowledgebase' ) ) {
$archive_template = plugin_dir_path(__FILE__) . '/templates/archive-knowledgebase.php';
}
return $archive_template;
}
add_filter( 'archive_template', 'get_custom_post_type_template' ) ;
Try that, then let me know the result. Thanks

Wordpress directly route to single Custom type result after searching

After searching (so ?s=foobar), I change the query :
function SearchFilter($query)
{
// If 's' request variable is set but empty
if(isset($_GET['s'])
&& empty($_GET['s'])
&& $query->is_main_query())
{
$query->is_search = true;
$query->is_home = false;
}
if ($query->is_search && !is_admin() ) {
$query->set('post_type',array('post','myFooBarCustomType'));
}
return $query;
}
But after the query, wordpress routes to the search.php page and I want to route to the single-customType.php page with the only-one result of my query. (a direct to the answer route with the good URL like www.mywebsite/myCustomType/foobar)
I only want the search.php page for a null result or more than 1.
Please help
Instead of your code, try adding this piece of code to your functions.php:
add_action('template_redirect', 'search_redirect_to_first_result');
function search_redirect_to_first_result() {
if (is_search()) {
if (have_posts()) {
the_post();
wp_redirect(get_permalink());
exit;
}
}
}

Redirect user after first login in wordpress?

This code checks if a user is logging in for the first time, that is after registration. I want to redirect him to a custom page if so. Otherwise, redirect him to the homepage or admin page.
function mylogin_redirect() {
global $user_ID;
if( $user_ID ) {
$user_info = get_userdata( $user_ID );
// If user_registered date/time is less than 48hrs from now
// Message will show for 48hrs after registration
if ( strtotime( $user_info->user_registered ) > ( time() - 172800 ) ) {
header("Location: http://example.com/custompage");
} elseif( current_user_can( 'manage_options' )) {
header("Location: http://example.com/wp-admin/");
} else {
header("Location: http://example.com/");
}
}
}
add_action('wp_head', 'mylogin_redirect');
But it doesn't work? My guess is it doesn't get hooked into wp_head...
I tried the following using login_redirect filter:
function mylogin_redirect($redirect_to, $url_redirect_to = '', $user = null) {
global $user_ID;
if( $user_ID ) {
$user_info = get_userdata( $user_ID );
// If user_registered date/time is less than 48hrs from now
// Message will show for 48hrs after registration
if ( strtotime( $user_info->user_registered ) > ( time() - 172800 ) ) {
return get_bloginfo('url') . "/custompage/";
} elseif( current_user_can( 'manage_options' )) {
return admin_url();
} else {
return get_bloginfo('url');
}
}
}
add_filter('login_redirect', 'mylogin_redirect');
Though it logs me in, it doesn't get me anywhere but to http://example.com/wp-login.php instead with a blank page.
UPDATE:
Ok, I don't know what's happening. Using the filter hook, I can get to the intended destination only after second login. Well not really second login but on the second click of the login button. I did it like so: enter credentials -> login -> (wrong page) -> hit back button -> enter credentials again -> login -> (correct page). Weird.
You need to adjust your filter call like so;
// filter name, callback, priority, accepted args
add_filter('login_redirect', 'mylogin_redirect', 10, 3);
Redirecting users on first login in WordPress: Cookie-based solution & User meta table based solution

Resources