I am trying to input a dashboard (simple html & css) above the page title on my wordpress site but only for the pages with the ID 777, 880 and 374.
I am assuming I need to make a function using is_page and then add that function to my page.php file but I can't seem to work it out.
Thanks for any help,
Scott
You could add logic like this to your page.php
<?php
if(is_page(777) || is_page(880) || is_page(374)) {
// Special Page Content Goes Here...
}
?>
** This will only work if the ID's are actually pages and not posts. Otherwise the conditional logic would be different.
Hold an array somewhere in your application with the allowed users (if just a few and not growing):
$allowedUsers = array('777' => true, '880' => true, '374' => true);
$userId = [current user id];
if($allowedUsers[$userId]) {
// Display dashboard content
}
If you have a more advanced user management system you should instead have priviliges and/or roles set for each specific user.
Related
I have a question and I can't find a solution yet, I apologize if I can't be clear on my question, but follow below.
In October, when I am creating a navigation menu, the parent menu forces me to take a route, however, many times the user cannot have permission for a certain page, example below in the image.
Possible solution:
Create a route and put several "ifs" in it to redirect the user, the problem with this is that the work would be extremely repetitive for each plugin ...
My question would be, how could I create a route that would automatically identify what permission the user would have at the moment and redirect to the correct page in a dynamic way for each plugin?
Or was the solution I indicated the only way?
I have one solution in mind.
Just make One extra fake controller and make index action of that controller.
In this index action we can have our own permission check and based on that we can redirect user to our predefined location.
Now for main menu just point URL to => myplugin/fakecontroller/index
this controller has no permission means its public so every one can use it.
Now in index action check permission of current user and based on that you can select redirect url.
// in fakecontroller's index action
$redirectUrl = 'default-url';
if($this->user->hasAccess('bla-bla')) {
$redirectUrl = 'some-other-url';
}
return \Redirect::to($redirectUrl);
To make it more dynamic with multiple plugins
To make it more dynamic you can just pass id of plugin to index action. make sure plugin url will be same for all other plugins. we just change id
Any how you have to use if statements because you want custom behaviour and i don't think we can add those info in to backend and if we try to implement that it will take too long. So, for now I have other solution which can make it little bit easy with multiple plugins.
for ex.
for plugin-A main menu url could be myplugin/fakecontroller/index/plugin-A
for plugin-B main menu url could be myplugin/fakecontroller/index/plugin-B
// fakecontroller
$pluginWiseRedirect = [
'pluginA' => [
'permission-name' => 'redirect-url-A',
'bla-bla' => 'new-url-A'
],
'pluginB' => [
'permission-name' => 'redirect-url-B',
'bla-bla' => 'new-url-B'
],
];
// in fakecontroller's index action
public function index($pluginId = 'pluginA') {
$redirectUrl = 'default-url';
$permissionName = 'permission-name';
if($this->user->hasAccess($permissionName)) {
// please make check here for key exist
$redirectUrl = $this->pluginWiseRedirect[$pluginId][$permissionName];
}
$permissionName = 'bla-bla';
if($this->user->hasAccess($permissionName)) {
// please make check here for key exist
$redirectUrl = $this->pluginWiseRedirect[$pluginId][$permissionName];
}
return \Redirect::to($redirectUrl);
}
if any doubts please comment.
I'm looking for a piece of code to add to my wordpress site to redirect certain users based on their role but only when they get to a certain page.
I need this to show a specific page to a specific user role and not the one others get. A plugin won't do in my scenario.
I'm not just looking for a redirect in php.
I need to make something that will work in 3 steps:
First I need to check if the user/visitor is on the desired page.
After that I need to check for a specific user role (I got this covered).
And at last to redirect (got that coverd to).
I have no idea how to accomplish the first step inside the functions.php and if my flow even makes sense..
Edit: Solved!
add_action('template_redirect', 'redirect_user_role');
function redirect_user_role()
{
if(current_user_can('subscriber') && is_page(' ID, Slug or name here '))
{
wp_redirect('https://www.example.nl');
}
}
First you need to get role of user,
<?php
global $current_user, $wpdb;
$role = $wpdb->prefix . 'capabilities';
$current_user->role = array_keys($current_user->$role);
$role = $current_user->role[0]; //user role
$page_title = $post->post_title; //get title f page
if($role=='subscriber' && $page_title=="aboutus") // compare
{
wp_redirect('http://www.google.com'); //navigate if so
}
?>
I'm using Peter's Login Redirect in Wordpress to redirect each User/Role to its "Personal Page".
It's all working great, but I would like to create a link where any user could click and be redirected back to its Login Redirect's page.
Right now, when a user login it goes to their "Personal Page", but when clicked into another link, there is no way to come back to it.
You should be able to use the redirect_to_front_page($redirect_to, $requested_redirect_to, $user) that is part of the plugin. It is primarily used as part of the login_redirect filter, so you can see more info about the arguments there. To use outside of this filter you could do something like the following, passing in false for the redirect URLS and detect if it is updated via your rules.
$user_id = get_current_user_id();
$user = get_user_by( 'id', $user_id );
if ( false !== ( $redirect_url = redirect_to_front_page( false, false, $user ) ) ){
// $redirect_url is the custom URL
} else {
// there is no custom URL
}
To solve it I add some code into the functions.php to make able to run PHP code inside widgets.
This is the code that I add into functions.php
add_filter('widget_text','execute_php',100);
function execute_php($html){
if(strpos($html,"<"."?php")!==false){
ob_start();
eval("?".">".$html);
$html=ob_get_contents();
ob_end_clean();
}
return $html;
}
After this I just add the HTML/PHP code to create a link that would redirect the current user to its personal page. I placed this code inside the text the should be presented when the user is logged in of the login widget plugin that I'm using.
Your Page
So That's it. If necessary more explanation about what I've done, please leave a comment below.
I've been looking for hours trying to find a straightforward answer to this but I just can't seem to find anything out there which tackles this issue. There are numerous examples for rewriting standard URLs, but nothing in-terms of rewriting pages from within a custom plugin.
I've created a custom plugin with a special directory in it called test. The test directory has several pages which are blah.php and shmeh.php. Is it possible to create rewrite rules to these pages i.e. http://example.com/blah, http://example.com/shmeh
Thank you very much for all the help.
So essentially what you are looking for, is to have Wordpress load a specific page template (blah.php) when the user visits http://example.com/blah/. Also, you need it to to be completely generated in your plug-in without actually creating any pages or posts. You are on the right track. What you want to do here is create a rewrite rule that utilizes a specific query var to load the page. Here it is step by step:
Step 1: Rewrite rules
http://codex.wordpress.org/Function_Reference/add_action
http://codex.wordpress.org/Plugin_API/Action_Reference/init
http://codex.wordpress.org/Rewrite_API/add_rewrite_rule
/* Rewrite Rules */
add_action('init', 'yourpluginname_rewrite_rules');
function yourpluginname_rewrite_rules() {
add_rewrite_rule( 'blah/?$', 'index.php?blah=true', 'top' );
}
So in the above code, your just telling Wordpress that example.com/blah/ should be treated as if the user were visiting example.com/index.php?blah=true. Now "blah" is a query var that you will set up in the next function below. Query vars are just what Wordpress calls $_GET variables. However they must be registered with Wordpress so it can properly store the value (which is "true" in this case). We will do that below in the next function. It's also worth noting the third setting in the function "top", which gives this rewrite rule priority over other rewrite rules. Alternatively, setting it to "bottom" would check that no other rewrite rules match first before using this one.
Step 2: Query Vars
http://codex.wordpress.org/Function_Reference/add_filter
http://codex.wordpress.org/Plugin_API/Filter_Reference/query_vars
/* Query Vars */
add_filter( 'query_vars', 'yourpluginname_register_query_var' );
function yourpluginname_register_query_var( $vars ) {
$vars[] = 'blah';
return $vars;
}
In the code above, your just registering the query var "blah" with Wordpress. That way, when someone visits example.com/blah/, which your rewrite rule tells Wordpress to treat it like they are visiting example.com/index.php?blah=true, Wordpress actually bothers to save it's value (which is set to true). This is so you can use it in the next function, which loads the template you want when users visit your url.
Step 3: Template Include
http://codex.wordpress.org/Function_Reference/add_filter
http://codex.wordpress.org/Plugin_API/Filter_Reference/template_include
http://codex.wordpress.org/Class_Reference/WP_Query#Interacting_with_WP_Query
http://codex.wordpress.org/Function_Reference/plugin_dir_path
/* Template Include */
add_filter('template_include', 'yourpluginname_blah_template_include', 1, 1);
function yourpluginname_blah_template_include($template)
{
global $wp_query; //Load $wp_query object
$page_value = $wp_query->query_vars['blah']; //Check for query var "blah"
if ($page_value && $page_value == "true") { //Verify "blah" exists and value is "true".
return plugin_dir_path(__FILE__).'test/blah.php'; //Load your template or file
}
return $template; //Load normal template when $page_value != "true" as a fallback
}
In the code above, you are simply telling Wordpress to use your blah.php template any time the query var "blah" is present and set to true. In this case it is present because a user is visiting a url with a rewrite rule that utilizes the "blah" query var. The function checks if the "blah" query var is present, then checks if "blah" is set to true, and if so loads the template. Also note that the template path is test/blah.php. You are using plugin_dir_path(__FILE__) to relatively include the template based on the location of your plugin. That way it's relative as a plug-in should be.
Step 4: The Actual Template (blah.php)
http://codex.wordpress.org/Function_Reference/get_header
http://codex.wordpress.org/Function_Reference/get_footer
http://codex.wordpress.org/Function_Reference/get_sidebar
Your template file will need to load the header, footer, and sidebar itself. When using template_include, Wordpress will not automatically load the header and footer as if you were using a page template. Be sure your template file calls get_header(), get_footer(), and get_sidebar() so that you get your complete layout.
Notes
Hope this helps, I was searching for something similar the other day and had to figure it out the hard way. It's also worth noting that I have prefixed all of the functions as "yourpluginname_". You can change this to whatever you want your prefix to be, but be sure to update the function names to match as well.
I am using wordpress multisite to create blog in my main site. Wordpress multisite allows us to signup for either user or blog, but i need to create a blog and a user at the same time and the user created need to be assigned as the admin of the blog created. Well i created a user and set it as administrator by changing it's metadata (wp_capabilities, wp_user_level) but when i tried to login to the admin of the blog created then it says you don't have sufficient priviledge. Do anyone know what do i missing?
Thanks in advance.
You have to create a custom registration. Create a new page: registration. In the theme folder make a page-registration.php file, and in the file create your form and the registration function, that should go something like this:
if($_POST)
{
$data=$_POST;
$validateuser=wpmu_validate_user_signup($data['user_name'],$data['user_email']);
$validateblog=wpmu_validate_blog_signup($data['user_name'],$data['blog_title']);
$usererrors=$validateuser['errors']->errors;
$blogerrors=$validateblog['errors']->errors;
if(!$usererrors && !$blogerrors)
{
$meta = array ('lang_id' => 1,'public' => 1);
$meta = apply_filters( 'add_signup_meta', $meta );
$path='/'.$data['user_name'].'/';
wpmu_signup_blog($domain,$path,$data['blog_title'],$data['user_name'],$data['user_email'], $meta);
}
}
note: this is only an example where the new blogname is the same as your username
leave a comment if you need more detailed instructions or have any additional questions