So I am experimenting and creating my first Wordpress site, and this is my first stack overflow question :)
My end goal is to hide the Personal Options for subscribers/contributors in their profile page. It's just clutter that gets in the way.
I found this code written by someone who had a similar problem, and slightly edited it to make it work for me:
function remove_opt_start($adddiv) {
$gettitle = array('#<h2>Personal Options</h2>#');
$adddiv = preg_replace($gettitle, '<div class="hidden">', $adddiv,1);
return $adddiv;
}
function start_remove_opt_start() { ob_start("remove_opt_start"); }
function end_remove_opt_start() { ob_end_flush(); }
function remove_opt_end($addend) {
$getname = array('#<h2>Name</h2>#');
$addend = preg_replace($getname, '</div><h2>Name</h2>',$addend,1);
return $addend;
}
function start_remove_opt_end() { ob_start("remove_opt_end"); }
function end_remove_opt_end() { ob_end_flush(); }
add_action('admin_head','start_remove_opt_start');
add_action('admin_head','start_remove_opt_end');
add_action('admin_footer','end_remove_opt_start');
add_action('admin_footer','end_remove_opt_end');
?>
And it's great! But it also removes the options for admins, editors, and authors, which I do not want. So I found this code where someone was attempting to distinguish between users:
global $current_user;
get_currentuserinfo();
switch (true) {
case ( user_can( $current_user, "subscriber") ):
// Show Role
// Show Subscriber Image
break;
case ( user_can( $current_user, "contributor") ):
// Show Role
// Show Contributor Image
break;
case ( user_can( $current_user, "administrator") ):
// Show Role
// Show Administrator Image
break;
}
So basically, my question is thus:
How do I have combine the two codes so that the first code block is only called when the second code block detects that a user is a contributor or subscriber.
I was hoping it could be as simple as coping and pasting it inside the commented area, but it never is quite that easy. Unfortunately, I have very little knowledge of php, but I'm excited to learn :)
Related
I would like show the admin notice while post text is being edited (before Save post). I suppose admin_notice hook wont work in this (it doesn't for me). Any idea how to show some error message to alert the user that Post content is not going to be accepted ?
I prefer inline display like admin notice instead of popups that could be blocked.
Following doesn't work
function secure_oembed_filter($html, $url, $attr, $post_ID) {
if (strlen($html) > 0 && strpos($html, "http://")) {
//wp_die("Unsecure link in embeds", "Unsecured post");
} else {
add_action('admin_notices', array($this, 'show_error'));
}
return $html;
}
private function show_error () {
echo '<div class="error"><p>This is an error</p></div>';
}
In that case wp_remote_get() will work fine.
Create a notice.php file and uploaded it on my server.
Then I have added these code into my plugin and it works for me.
<?php
$remote_data = wp_remote_get( 'http://example.com/notice.php' );
$remote_body = wp_remote_retrieve_datat( $remote_data );
?>
<div class="notice">
<?php echo $remote_body ?>
</div>
Hope that helps.
Im new in wordpress.
I want to use two tables and display some content without creating pages or posts.
The matched url will be something like this:
/[category]/[image] - /nature/green-tree
My method was to check the url in index.php from theme and split the url and create a mini route system just for the gallery. But I think it is not the smartest idea.
I don't want to use gallery plugins because I'm already using one, and this is a change that I need to to.
What is the best method to do this?
I suppose you need custom rewrite rules. Here is how you can do it by yourself.
If you are using a theme then open the functions.php file and enter the following code, otherwise if you are using a plugin, place this code somewhere in your plugin, but make sure, that it is loading immediately.
function registerCustomUrl($rewrite)
{
$rewrites = $rewrite->rules;
$newRewrites = array();
$newRewrites['([^/]+)/([^/]+)/?$'] = 'index.php?gallery_category=$matches[1]&gallery_image=$matches[2]';
foreach($rewrites as $rk => $rv)
{
$newRewrites[$rk] = $rv;
}
$rewrite->rules = $newRewrites;
}
add_action('generate_rewrite_rules', 'registerCustomUrl');
function registerCustomQueryVars( $vars ) {
$vars[] = 'gallery_category';
$vars[] = 'gallery_image';
return $vars;
}
add_filter('query_vars', 'registerCustomQueryVars');
function myCustomTemplateRedirect()
{
global $wp_query;
if(
isset($wp_query->query_vars['gallery_category']) &&
!empty($wp_query->query_vars['gallery_category']) &&
isset($wp_query->query_vars['gallery_image']) &&
!empty($wp_query->query_vars['gallery_image'])
)
{
// Within your custom template file you are able to
// use any theme function like the get_header(), get_footer(), etc
require_once('path/to/your/custom/template/file.php');
exit(0);
}
}
add_action("template_redirect", 'myCustomTemplateRedirect');
Cannot find where to add validation for a checkbox on wordpress login form. I have an additional checkbox set up called 'terms' that I need the user to check each time they want to log in.
Problem is that I cannot stop wordpress logging in if they don't check it. Where it the login code.
There is also a plugin installed that may be complicating matters called them-my-login.
I have all the code in front of me, just tell me what I'm looking for.
I know this is fairly old but I just stumbled across it and was able to look at the codex and work out a solution. Hope this helps somebody. Thanks to #Jason for pointing in the right direction.
This code will need to be added to your theme's functions.php file:
<?php
// As part of WP authentication process, call our function
add_filter('wp_authenticate_user', 'wp_authenticate_user_acc', 99999, 2);
function wp_authenticate_user_acc($user, $password) {
// See if the checkbox #login_accept was checked
if ( isset( $_REQUEST['login_accept'] ) && $_REQUEST['login_accept'] == 'on' ) {
// Checkbox on, allow login
return $user;
} else {
// Did NOT check the box, do not allow login
$error = new WP_Error();
$error->add('did_not_accept', 'You must accept the terms and conditions' );
return $error;
}
}
// As part of WP login form construction, call our function
add_filter ( 'login_form', 'login_form_acc' );
function login_form_acc(){
// Add an element to the login form, which must be checked
echo '<label><input type="checkbox" name="login_accept" id="login_accept" /> I agree</label>';
}
The answer Patrick Moore gave did not work for me, but I did modify it to provide a valid solution. This may be because he answered it back in 2013, and now the code has changed. I changed the filter to login_form_middle, and modified the function at the end as a variable, and then passing the value back through a return:
<?php
// As part of WP authentication process, call our function
add_filter('wp_authenticate_user', 'wp_authenticate_user_acc', 99999, 2);
function wp_authenticate_user_acc($user, $password) {
// See if the checkbox #login_accept was checked
if ( isset( $_REQUEST['login_accept'] ) && $_REQUEST['login_accept'] == 'on' ) {
// Checkbox on, allow login
return $user;
} else {
// Did NOT check the box, do not allow login
$error = new WP_Error();
$error->add('did_not_accept', 'You must accept the terms and conditions' );
return $error;
}
}
// As part of WP login form construction, call our function
add_filter ( 'login_form_middle', 'login_form_acc' );
function login_form_acc(){
// Add an element to the login form, which must be checked
$termsLink = '<label><input type="checkbox" name="login_accept" id="login_accept" /> I agree</label>';
return $termsLink;
}
WordPress Filters/Actions are your friend.
Take a look at:
admin_init
wp_login
I'm using a very simple plugin called Semi Private Comments which does almost everything I need, it hides comments from other users and allows only the author of the comment and the admin to view the comment. My problem is the plugin allows any admin comment to be viewed by anyone. I would like it to keep the comment between the admin and any user a one on one conversation.
I really don't know PHP well enough to modify the plugins logic and was hopping for some help.
Here's the code.
if (current_user_can('edit_users') || // user is admin, or
$user_matched==1 || // user is original author, or
$comment->user_id == 1) // comment author is admin
{
return $content;
}
else
{
$hidden_comment_text = get_option('spc_hidden_comment_text');
return $hidden_comment_text;
}
}
else
{
return $content;
}
I think just removing the $comment->user_id == 1 should do the trick
if (current_user_can('edit_users') || // user is admin, or
$user_matched==1) // user is original author
{
return $content; // Only admins and authors of the comment can read
}
else
{
$hidden_comment_text = get_option('spc_hidden_comment_text');
return $hidden_comment_text;
}
Btw, the code snipped you posted is incomplete the if statement of the following part is missing
}
else
{
return $content;
}
I have created a wordpress plugin that has a filter on the_content, looking for a specific tag then outputting the plugin content in place of that tag.
I would now like to use rewrite rules to call the plugin and output the data within the template, but I'm not finding much help.
Can someone provide an example, or some guidance on how to add a rewrite rule using the built in wp methods and calling my methods in the plugin which outputs some content.
Ideally, I would like shop/ to be matched and then pass everything after shop to my dispatch method on my plugin so that I can have shop/category/shirts or shop/product/the-cool-shirt. My dispatch method would handle breaking apart the rest of the url and calling methods accordingly.
This can get rather interesting. I had to do this for a plugin at one point, I don't have it in front of me, so this it out of memory, but the general idea should be correct.
<?php
add_action('init', 'rewrite_rules');
function rewrite_rules() {
global $wp, $wp_rewrite;
$wp_rewrite->add_rule('(widget1|widget2|widget3)/([a-zA-Z0-9_-]{3,50})$', 'index.php?pagename=listing&category=$matches[1]&subcategory=$matches[2]', 'top' );
$wp->add_query_var( 'category' );
$wp->add_query_var( 'subcategory' );
$wp_rewrite->flush_rules();
}
?>
Using regular expressions is a monumental task in itself, I believe I used this site: http://tools.netshiftmedia.com/regexlibrary/ for help.
I also used FakePage plugin to actually display my custom "dynamic" pages, as I called them, but I suppose everything in WP is technically dynamic.
http://scott.sherrillmix.com/blog/blogger/creating-a-better-fake-post-with-a-wordpress-plugin/
Let me know if you need more help.
I did something very similar not long ago, and I did it by cheating.
If you find the built in rewrite rules too complicated or unable to do the job, you may find it easier to catch the request and filter the results. A simplified version:
add_action('parse_request', 'my_parse_request');
function my_parse_request (&$wp) {
$path = $wp->request;
$groups = array();
if (preg_match("%shop/product/([a-zA-Z0-9-]+)%", $path, $groups)) {
$code = $groups[1];
$product = get_product($code); // your own code here
if (isset($product)) {
add_filter('the_posts', 'my_product_filter_posts');
}
}
}
function my_product_filter_posts ($posts) {
ob_start();
echo "stuff goes here"; // your body here
$content = ob_get_contents();
ob_end_clean();
return array(new DummyResult(0, "Product name", $content));
}
To explain:
The action on parse_request is called before the database lookup. Based on the URL, it installs the other actions and filters.
The filter on posts replaces the results of the database lookup with fake results.
DummyResult is a simple class that has the same fields as a post, or just enough of them to get away with it:
class DummyResult {
public $ID;
public $post_title;
public $post_content;
public $post_author;
public $comment_status = "closed";
public $post_status = "publish";
public $ping_status = "closed";
public $post_type = "page";
public $post_date = "";
function __construct ($ID, $title, $content) {
$this->ID = $ID;
$this->post_title = $title;
$this->post_content = $content;
$this->post_author = get_default_author(); // implement this function
}
}
There's a lot of homework left for the reader in the above, but it's an ugly, working approach. You'll probably want to add a filter for template_redirect, to replace the normal page template with a product-specific one. And you may need to adjust the URL regex if you want pretty permalinks.