Trying to get a simple plugin and got error in my first steps..
<?php
/*
Plugin Name:!test
*/
require_once(includes_url() . '/pluggable.php');
function is_user_logged_in() {
$user = wp_get_current_user();
return $user->exists();
}
echo is_user_logged_in();
?>
You can't call function directly in your plugin file. Instead of it, call it inside the hook, for ex.:
function my_shortcode_func( $atts ) {
$user = wp_get_current_user();
return $user->exists();
}
add_shortcode( 'my_shortcode', 'my_shortcode_func' );
Related
I'm trying to customize a shortcode put inside a custom plugin, but I can't get the user id, it always returns me 0.
It might also be okay to understand the role with current_user_can, but any information is always empty.
Here the code:
add_action( 'plugins_loaded', 'check_current_user' );
function check_current_user() {
// Your CODE with user data
global $current_user;
$current_user = wp_get_current_user();
return $current_user->ID;
}
function appp_hide_content_shortcode( $atts, $content = '' ) {
if( class_exists('AppPresser') && AppPresser::is_app() )
return check_current_user();
else
return $content;
}
add_shortcode('appp_hide_content', 'appp_hide_content_shortcode');
Please try this
/**
* Hide content on app.
*
* Use this shortcode to hide content when viewed using the app.
*
* Use:
* [appp_hide_content]This content will not appear on the app.[/appp_hide_content]
*/
function appp_hide_content_shortcode( $atts, $content = '' ) {
ob_start();
//GET CURRENT USER ID
global $current_user;
$current_user = wp_get_current_user();
echo $current_user->ID;
if (current_user_can('free')){
if( class_exists('AppPresser') && AppPresser::is_app() )
echo '';
else
echo $content;
}
$sc_html = ob_get_contents();
ob_end_clean();
return $sc_html;
}
add_shortcode('appp_hide_content', 'appp_hide_content_shortcode');
You need to check the user after the user is loaded. I would hook to init. You can't call the pluggable functions directly in a plugin without delaying the execution. The simpler solution to your problem would be to include your shortcode in your theme's functions.php, rather than in a plugin. But below will execute.
add_action( 'init', 'check_current_user' , 999);
function check_current_user() {
// This returns current user id
return get_current_user_id();
}
function appp_hide_content_shortcode( $atts, $content = '' ) {
if( class_exists('AppPresser') && AppPresser::is_app() ){
// This is only returning an integer here.
return check_current_user();
} else {
return $content;
}
}
add_shortcode('appp_hide_content', 'appp_hide_content_shortcode');
I'm creating a plugin OOP for wordpress. The plugin creates a new custom post type called teams. Within a team page a could use the shortcode [program] to generate some predefault html code. Also i've created custom fields with new meta boxes.
The problem however is: when i'm entering the page thats calling the plugin, equal the team page with the sortcode i need to get post id within my plugin to retrieve get_post_meta().
I've tried the following things:
public function __construct(){
// not working
$post;
$post->ID;
// not working
global $wp_query;
$post_id = $wp_query->post->ID;
$post = get_post( $post_id );
// not workiing
echo '<pre>';
print_r('post_id:' . get_the_ID());
echo '</pre>';
}
How could i receive the custom post id within my plugin when i visited the page from frontend (so the plugin is called, runs the shortcode)
My main class gets loaded like this:
function run_plugin() {
$plugin = new MyPlugin();
$plugin->run();
}
run_plugin();
Within MyPlugin the constructor looks like
public function __construct() {
if ( defined( 'PLUGIN_NAME_VERSION' ) ) {
$this->version = PLUGIN_NAME_VERSION;
} else {
$this->version = '1.0.0';
}
$this->plugin_name = 'MyPlugin';
if(!empty(get_option($this->plugin_name))){
$this->clientID = get_option($this->plugin_name)['client_id'];
}
$this->load_dependencies();
$this->set_locale();
$this->define_admin_hooks();
$this->define_public_hooks();
$this->define_shortcodes();
}
If your plugin constructor is getting called too early, the post data won't be set up and ready to use.
You'll need to hook into one of WPs actions that run after everything is ready. The init action should be enough for the post data, but depending on what else you need you can hook into wp_loaded, as it doesn't run until after WordPress is fully loaded.
function run_plugin() {
$plugin = new MyPlugin();
$plugin->run();
}
/* run_plugin(); */ // <- instead of this....
add_action( 'wp_loaded','run_plugin' ); // <- ...do this
Try to define post as global like
global $post
How do I check if a user is admin before running a function in a Wordpress plugin. Something that seems like it should be trivial is a pain.
I've looked at dozens of posts online and can't find one thing that works. I tried the following for example (among half a dozen other things) which is a function in a plugin :
global $current_user;
if ( $current_user->role[0]=='administrator' ) {
function remove_post_metaboxes() {
remove_meta_box( 'formatdiv','album','normal' );
}
add_action('admin_menu','remove_post_metaboxes');
}
<?php if (current_user_can( 'manage_options' )) {
// do stuff
} ?>
$current_user = wp_get_current_user();
// print_r($current_user);
if ($current_user->has_cap('administrator')) {
// do something
echo 'is an admin';
}
So I was doing this wrong, the answer supplied by ReLeaf is partially correct but nobody pointed out that instead of trying to wrap the function like the example I gave in the original question and is why i was getting a blank admin screen :
global $current_user;
if ( $current_user->role[0]=='administrator' ) {
function remove_post_metaboxes() {
remove_meta_box( 'formatdiv','album','normal' );
}
add_action('admin_menu','remove_post_metaboxes');
}
I should have had the conditional inside the function instead :
function remove_post_metaboxes() {
global $current_user;
if ( $current_user->role[0]=='administrator' ) {
remove_meta_box( 'formatdiv','album','normal' );
}
}
add_action('admin_menu','remove_post_metaboxes');
So that's how it's done, thanks me for pointing it out to me ;)
how to get a product by id outside the loop
this is my function:
function rakhsh_product_info($id){
$result = get_product( $id );
die($result);
}
get Call to undefined function get_product() error
Try this:
if ( function_exists( 'get_product' ) ) {
$result = get_product( $id );
} else {
$result = new WC_Product( $id );
}
Hope that helps.
All of these errors indicate that your plugin is loaded before Woocommerce. Place the calls to woocommerce functions at least in plugins_loaded action or later. Example from wordpress.org:
<?php
add_action( 'plugins_loaded', 'my_plugin_override' );
function my_plugin_override() {
// your code here
}
?>
Check here for list of available action hooks and action execution order.
When I attempt the following I get Warning: Cannot modify header information - headers already sent by......
I'm trying to require a user to be logged in before they access my page with the shortcode on it.
What am I missing? Thanks a million for any help you can offer.
add_shortcode( 'guest-posts', 'guestposts_shortcode' );
function guestposts_shortcode( $atts ) {
auth_redirect();
}
It may work if you parse the post content before to render it. Then you should check if you find the shortcode inside the content.
Here is a small generic function to check it :
function has_shortcode($shortcode = '') {
global $post;
if (!$shortcode || $post == null) {
return false;
}
if ( stripos($post->post_content, '[' . $shortcode) !== false ) {
return true;
}
return false;
}
Now we have to create a function that will check for our specific shortcode :
function unlogged_guest_posts_redirect() {
if(has_shortcode('guest-posts') && !is_user_logged_in()) {
auth_redirect();
}
}
Then we have to hook our function (I think this can work in "wp" hook, but you can try anohter if it does not) :
add_action('wp', 'unlogged_guest_posts_redirect');
To finish, we have to ensure that the shortcode won't echo anything :
add_shortcode( 'guest-posts', 'guestposts_shortcode' );
function guestposts_shortcode( $atts ) {
return false;
}
Actually we are dealing with shortcodes but we're not using the WordPress shortcode API. This functionnality should be done using a custom field, it would be simpler !
You could instead create a special category, and hook into template_redirect:
add_filter('template_redirect', function() {
if (is_single() && in_category('special') && !is_user_logged_in())
wp_redirect(site_url('/wp-login.php'));
});