I have made a public account in wordpress which I will send to 100 users.
So the login would be:
Username: public
Password: 123example
The only thing I want is to hide the profile page for this specific user account so they can't change password, emailadress, etc.
How to achieve this? Maybe change some php?
The last portion in #aSeptik's answer could be a little more WP friendly.
function force_profile_redirect() {
global $pagenow, $current_user;
get_currentuserinfo();
if ($pagenow == 'profile.php' && $current_user->user_login == 'public') {
wp_redirect(home_url());
}
}
add_action('admin_init', 'force_profile_redirect');
this script cover all the aspects of the question, read the code comments for further explanation.
<?php
/**
* this make sure the public user where redirected
* to home instead of profile page
*/
function redirect_user_to($redirect_to, $request, $user)
{
global $user;
if ($user->user_login == 'public') {
return home_url();
}
else {
return home_url("/wp-admin/");
}
}
add_filter('login_redirect', 'redirect_user_to', 10, 3);
/**
* this remove the profile links from
* the top nav menu
*/
function remove_edit_profile()
{
global $wp_admin_bar, $current_user;
get_currentuserinfo();
if ($current_user->user_login == 'public') {
$wp_admin_bar->remove_menu('edit-profile');
$wp_admin_bar->remove_menu('my-account-with-avatar');
$wp_admin_bar->remove_menu('my-account');
}
}
add_action('wp_before_admin_bar_render', 'remove_edit_profile', 0);
/**
* this remove the "Site Admin" link from
* the WP meta widget, usually placed in
* the side bar.
*/
function my_unregister_widgets()
{
unregister_widget('WP_Widget_Meta');
register_widget('MY_Widget_Meta');
}
add_action('widgets_init', 'my_unregister_widgets');
class MY_Widget_Meta extends WP_Widget
{
function MY_Widget_Meta()
{
$widget_ops = array(
'classname' => 'widget_meta',
'description' => __("Log in/out, admin, feed and WordPress links"),
);
$this->WP_Widget('meta', __('Meta'), $widget_ops);
}
function widget($args, $instance)
{
extract($args);
$title = apply_filters('widget_title', empty($instance['title']) ? __('Meta') : $instance['title']);
echo $before_widget;
if ($title) {
echo $before_title.$title.$after_title;
}
?>
<ul>
<?php
global $current_user;
get_currentuserinfo();
if ($current_user->user_login == 'public') {
}
else {
wp_register();
}
?>
<li>
<?php wp_loginout();?>
</li>
<li>
<a href="<?php bloginfo('rss2_url');?>" title="<?php echo esc_attr(__('Syndicate this site using RSS 2.0'));?>">
<?php _e('Entries <abbr title="Really Simple Syndication">RSS</abbr>');?></a>
</li>
<li>
<a href="<?php bloginfo('comments_rss2_url');?>" title="<?php echo esc_attr(__('The latest comments to all posts in RSS'));?>">
<?php _e('Comments <abbr title="Really Simple Syndication">RSS</abbr>');?></a>
</li>
<li>
WordPress.org
</li>
<?php wp_meta();?>
</ul>
<?php
echo $after_widget;
}
}
/**
* this prevent from non authorized user ( public )
* to pointing to the profile page by writing into
* the address bar.
*/
function force_profile_redirect()
{
global $pagenow, $current_user;
if (strtolower($current_user->user_login) == 'public') {
wp_redirect(home_url());
}
}
add_action('admin_init', 'force_profile_redirect');
?>
You'd need to modify your profile page code, to make it not show the editable areas, and not run the "update profile" action, if the user ID is [xyz].
For the page which actually does the updating of the profile, you can just put at the top something like
// Change this line to match however you identify your logged-in user
// And change the id number to the ID of the public user
global $current_user;
get_currentuserinfo();
if ($current_user->ID == 1)
{
// Stop them seeing this page
header('Location: index.php');
// And for good measure
die();
}
For the page on which they can change the profile fields before they submit the form, you can do something like this
// Change this line to match however you identify your logged-in user
// And change the id number to the ID of the public user
global $current_user;
get_currentuserinfo();
if ($current_user->ID == 1)
{
// Say no
echo '<p>You cannot edit your profile on this account.</p>';
// And for good measure
die();
}
Without seeing your code, it's hard to be more specific, but this should work at a push, even if it's not exactly how you want it to work.
Related
I am fairly new to coding and need a little guidance. I am using the following .php:
// [current_user_display_name]
function display_current_user_display_name () {
$user = wp_get_current_user();
$display_name = $user->display_name;
return $user->display_name;
}
add_shortcode('current_user_display_name', 'display_current_user_display_name');
and would like to style the text with CSS. I'm not sure how to do this, I tried this:
.display_current_user_display_name
{
color:#ffffff;
font-family:lato;
font-size:16px;
font-weight:regular;
}
but it did not work. Thanks!
This could be the PHP code for creating the WordPress shortcode.
Using this shortcode [current_user_display_name] will display an HTML span with the CSS class user-display-name.
We only show this if the user is logged in.
/**
* Current user display name shortcode.
*/
function get_current_user_display_name() {
// Check that the user is logged in.
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
return sprintf( '<span class="user-display-name">%s</span>', $user->display_name );
}
// If user is not logged in don't return anything.
return;
}
add_shortcode( 'current_user_display_name', 'get_current_user_display_name' );
Below is an example of how to style the CSS class .user-display-name:
.user-display-name {
/* CSS code here */
}
So I have a Custom WordPress Theme I am developing and upon installation of it in a new WordPress setup I would like a MENU auto-generated along with 3 pages, i.e:
Products
Policy
Services
that is my theme will heavily depend on.
I will already have 3 template pages made for these in the custom theme files, i.e:
page-products.php
page-policy.php
page-services.php
So when the theme is installed these are auto-created so it's a no brainer for the client.
I know Twig but very little PHP and if some could write this up so I could just drop it into my functions.php file.
I would be very, very grateful and many thanks!!
EDIT
Additional questions
Also I will be having a Home and a News page auto created as well. Would there be a quick code to auto set these up as the default Homepage and default Posts page? Or is it more complicated than that?
You could use after_switch_theme action hook and wp_insert_post function to achieve what you're looking for.
after_switch_themeDocs
wp_insert_postDocs
So to generate these three pages, first, we create a function that incorporates wp_insert_post function. Our custom function will take two arguments and first checks whether the page you're trying to create exists or not. If the page exists it will return false, otherwise, it'll create the page and return the id of that page.
The following code goes to the functions.php file of your active theme.
function your_theme_create_page($page_title, $page_content)
{
$page_obj = get_page_by_title($page_title, 'OBJECT', 'page');
if ($page_obj) {
return false;
exit();
}
$page_args = array(
'post_type' => 'page',
'post_status' => 'publish',
'post_title' => ucwords($page_title),
'post_name' => strtolower(trim($page_title)),
'post_content' => $page_content,
);
$page_id = wp_insert_post($page_args);
return $page_id;
}
Now, on after_switch_theme action hook we run another custom function. This time we will create an associative array to hold our page titles as well as page contents. Then we feed that array to the function we created above using a foreach loop.
The following code also goes to the functions.php of your active theme.
add_action('after_switch_theme', 'my_custom_pages_on_theme_activation');
function my_custom_pages_on_theme_activation()
{
$pages_array = array(
'products page' => '<h3>Hello from products page</h3>',
'policy page' => '<h3>Hello from policy page</h3>',
'services page' => '<h3>Hello from services page</h3>'
);
foreach ($pages_array as $page_title => $page_content) {
$current_page = your_theme_create_page($page_title, $page_content);
if (false != $current_page) {
add_action('admin_notices', function () use ($page_title) {
?>
<div class="notice notice-success is-dismissible">
<p><?php echo "Done! {$page_title} has been created"; ?></p>
</div>
<?php
});
} else {
add_action('admin_notices', function () use ($page_title) {
?>
<div class="notice notice-warning is-dismissible">
<p><?php echo "{$page_title} was not created! Check whether it already exists"; ?></p>
</div>
<?php
});
}
}
}
Note that i used admin_notices action hook to give the user a visual message.
if generating those pages was successful, then user sees the following messages:
If, on the other hand, those pages were not generated or they already exist, then the user will see the following messages:
Now we have our pages created, you could see them on the pages menu on the admin screen:
And here's the page content for example:
Now, you could take this one step further and create more complex templates and feed them to that assortative array.
So for example, you have page-products.php, page-policy.php and page-services.php files inside a folder called, let's say, inc. So your folder structure would be something like this:
Your-theme-folder
|
|css-folder
| |
| some-css.css
|
|javascript-folder
| |
| some-js.js
|
|inc-folder
|
page-products.php
page-policy.php
page-services.php
Then your associative array would be something like this:
add_action('after_switch_theme', 'my_custom_pages_on_theme_activation');
function my_custom_pages_on_theme_activation()
{
$products_page_title = 'products page';
$policy_page_title = 'policy page';
$services_page_title = 'services page';
$products_page_content = file_get_contents(__DIR__ . '\inc\page-products.php');
$policy_page_content = file_get_contents(__DIR__ . '\inc\page-policy.php');
$services_page_content = file_get_contents(__DIR__ . '\inc\page-services.php');
$pages_array = array(
$products_page_title => $products_page_content,
$policy_page_title => $policy_page_content,
$services_page_title => $services_page_content
);
foreach ($pages_array as $page_title => $page_content) {
$current_page = your_theme_create_page($page_title, $page_content);
if (false != $current_page) {
add_action('admin_notices', function () use ($page_title) {
?>
<div class="notice notice-success is-dismissible">
<p><?php echo "Done! {$page_title} has been created"; ?></p>
</div>
<?php
});
} else {
add_action('admin_notices', function () use ($page_title) {
?>
<div class="notice notice-warning is-dismissible">
<p><?php echo "{$page_title} was not created! Check whether it already exists"; ?></p>
</div>
<?php
});
}
}
}
Let me know if you have any question.
Answer to the additional questions
Yes it's feasible to do so. Wordpress keeps track of the blog posts page under page_for_posts option. It also store the info for front page under page_on_front and show_on_front options. So to assign your pages to the blog and front pages, you'll update those options.
So when the custom function fires off in the after_switch_theme action hook, after the foreach loop you could add the following code to assign your pages:
$blog_page = get_page_by_title('news', 'OBJECT', 'page');
if ($blog_page) {
update_option('page_for_posts', $blog_page->ID);
}
$front_home_page = get_page_by_title('home', 'OBJECT', 'page');
if ($front_home_page) {
update_option('page_on_front', $front_home_page->ID);
update_option('show_on_front', 'page');
}
get_page_by_titleDocs
So the entire custom function for the after_switch_theme action hook would be something like this:
add_action('after_switch_theme', 'my_custom_pages_on_theme_activation');
function my_custom_pages_on_theme_activation()
{
$pages_array = array(
'products page' => '',
'policy page' => '',
'services page' => ''
);
foreach ($pages_array as $page_title => $page_content) {
$current_page = your_theme_create_page($page_title, $page_content);
if (false != $current_page) {
add_action('admin_notices', function () use ($page_title) {
?>
<div class="notice notice-success is-dismissible">
<p><?php echo "Done! {$page_title} has been created"; ?></p>
</div>
<?php
});
} else {
add_action('admin_notices', function () use ($page_title) {
?>
<div class="notice notice-warning is-dismissible">
<p><?php echo "{$page_title} was not created! Check whether it already exists"; ?></p>
</div>
<?php
});
}
}
$blog_page = get_page_by_title('news', 'OBJECT', 'page');
if ($blog_page) {
update_option('page_for_posts', $blog_page->ID);
}
$front_home_page = get_page_by_title('home', 'OBJECT', 'page');
if ($front_home_page) {
update_option('page_on_front', $front_home_page->ID);
update_option('show_on_front', 'page');
}
}
im developing a page and using this free simple Favorite Post plugin to add favorites to users. currently its working for logged in users only. i need to show it to non logged users and when click on its button redirect to login or register page and after logged in redirect to same page or custom page.
this is for show favorite button code on front end : or short code :[favorite-post-btn]
this is for short for code display favorited posts :[favorite-post]
if anyone can help me big it would me grateful..! thank you i will put the button code here :
/**
* Favorite post link button
*
* #param int $post_id
* #return void
*/
function link_button( $post_id ) {
if ( !is_user_logged_in() ) {
return;
}
$status = $this->get_post_status( $post_id, get_current_user_id() );
?>
<a class="wpf-favorite-link" href="#" data-id="<?php echo $post_id; ?>">
<?php if ( $status ) { ?>
<span class="wpf-favorite"> </span> <?php _e( 'Remove from favorite', 'wfp' ); ?>
<?php } else { ?>
<span class="wpf-not-favorite"> </span> <?php _e( 'Add to favorite', 'wfp' ); ?>
<?php } ?>
</a>
<?php
}
you can add some if statmen like
if (isset($_SESSION['username'])
or something like that, which if the condition is true makes you click the button, otherwise it redirects you to the login page
You are looking for https://developer.wordpress.org/reference/functions/wp_redirect/
Checking: Redirect after Login on WordPress
This has been answered here: https://stackoverflow.com/a/65380999/5214689
I have wordpress theme which make and post new div with unique id for episode <div class = "post-2122 episode type-episode status-publish has-post-thumbnail hentry"> and how can i change border in css only for specific user and dependable on php variable? post-xxxx is uniquefor every episode.
Maybe you could also add the user ID in body_class()
add_filter( 'body_class', 'my_class_names' );
function my_class_names( $classes ) {
// add 'class-name' to the $classes array
global $current_user;
$user_ID = $current_user->ID;
$classes[] = 'user-id-' . $user_ID;
// return the $classes array
return $classes;
}
If you want to display a special border on specific user when is logged, you can try :
<?php
if ( is_user_logged_in() ) {
echo '<div class="is-logged">';
} else {
echo '<div>';
}
?>
And then apply a CSS to .is-logged
you can use:
<div class="post-<?php $postid = get_the_ID(); ?> episode type-episode status-publish has-post-thumbnail hentry">
here is the reference how to get post id
hope your problem will be solve.!
You can use get_current_user_id() to get the logged in ID of the user.
<?php $user = 'user_' . get_current_user_id(); ?>
<div class="<?php echo $user; ?> post-2122 episode type-episode status-publish has-post-thumbnail hentry">
If the user is not logged in, $user will echo user_0, if they are logged in, $user will echo user_xx with their ID in place of xx.
Codex: https://developer.wordpress.org/reference/functions/get_current_user_id/
I am new with wordpress and I really appreciate this cms application. I have been working in creating my own plugin and I was stuck in this particular problem. I have created a text link that points to one my function I have created. But whenever I click this link "Add Category" it returns the "you do not have sufficient permissions to access this page." instead of a simple text "hello" text to be printed. What is the problem with this? I need somebody to help me. Here are my codes
product_category.php
<?php
/**
* Plugin Name: Categorized Product Plugin
* Plugin URI:
* Description: A plugin for categorizing products
* Author:
* Author URI:
* License: A "Slug" license name e.g. GPL2
*/
add_action('admin_menu',array('my_product','myplugin_menu'));
register_activation_hook( __FILE__, array('my_product','jal_install'));
global $jal_db_version;
$jal_db_version = "1.0";
class my_product{
function myplugin_menu(){
if( function_exists('add_management_page')){
$page = add_menu_page('product_zone','product Zone','administrator','myplugin_menu_display',array('my_product','category_adding_form'),plugins_url("img/logo_product.png", __FILE__));
}
}
function category_adding_form(){
include('categorized_product_form.php');
}
function test_link(){
echo "hello";
}
?>
add_form_link.php
<div class="wrap">
<?php
$path = 'admin.php?page=test_link';
$url = admin_url($path);
$link = "<a href='{$url}'>Add Category</a>";
echo $link;
echo "<h1>Product Zone</h1>";
?>
This
?page=test_link
does not have relation with
function test_link()
What you can do is reuse the same page. Put this code in your categorized_product_form.php file:
<div class="wrap">
<?php
$path = 'admin.php?page=myplugin_menu_display';
$url1 = admin_url( $path. '&test_link=true' );
$url2 = admin_url( $path );
if( isset( $_GET['test_link'] ) )
{
echo '<h1>hello world</h1>';
echo "<a href='{$url2}'>back</a>";
}
else
{
echo "<h1>Product Zone</h1>";
echo "<a href='{$url1}'>Add Category</a>";
}
Related: How to enable additional page in WordPress custom plugin?