Created
April 18, 2017 13:30
-
-
Save AladinDridi/b2aac59c7b0a9369a53d3ad7f85e8901 to your computer and use it in GitHub Desktop.
Call function to change role by html5 submit button
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<body> | |
<form action="localhost/change_role_7%C3%A8me.php" method="get"> | |
<input type="submit" name="on" value="on"> | |
<input type="submit" name="off" value="off"> | |
</form> | |
</body> | |
</html> | |
<?php | |
if(isset($_GET['on'])) { | |
onFunc(); | |
} | |
if(isset($_GET['off'])) { | |
offFunc(); | |
} | |
function onFunc(){ | |
global $current_user; | |
get_currentuserinfo(); | |
if(user_can($current_user ,'7eme') ) | |
{ | |
// Remove role | |
$current_user->remove_role( '7eme' ); | |
// Add role | |
$current_user->add_role( 'contributor' ); | |
} | |
echo "..Merci"; | |
} | |
function offFunc(){ | |
echo "Bonne chance dans le prémiére fois"; | |
}?> | |
--------------------------- | |
page change_role.php | |
<?php | |
function update_roles() | |
{ | |
global $wpdb; | |
// Get the author | |
$author = wp_get_current_user(); | |
// Not sure if $author and $u are the same object I suspect they are. | |
// so this may not be necessary, but I found this code elsewhere. | |
// You may be able to do without this and just replace $u with $author later in the code. | |
// Get post by author | |
$posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_author = " . $author->ID ); | |
$numPost = count($posts); | |
// Do the checks to see if they have the roles and if not update them. | |
if($numPost > 0 && $numposts <= 10 && current_user_can('7eme') ) | |
{ | |
// Remove role | |
$author->remove_role( '7eme' ); | |
// Add role | |
$author->add_role( 'contributor' ); | |
} | |
} | |
add_action('publish_post', 'update_roles'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment