WordPress AJAX is_admin is true, causing issues.










1















i'm trying to make a plugin for WordPress, which is has got an admin section for some basic settings, and also registers some shortcode to display some HTML, which is basically a form.



Here is my main plugin file, plugins/my-plugin/my-plugin.php:



 /**
* Plugin Name: Pathway
* Plugin URI: http://www.martynleeball.com/
* Description: Pathway integration.
* Version: 1.0
* Author: Martyn Lee Ball
* Author URI: https://www.martynleeball.com/
**/

define('PATHWAY_VERSION', '0.0.8');
define('PATHWAY_AUTHOR', 'Martyn Lee Ball');
define('PATHWAY__MINIMUM_WP_VERSION', '4.*');
define('PATHWAY_CONTACT', 'martynleeball@gmail.com');

add_action(
'plugins_loaded',
array ( Pathway::get_instance(), 'plugin_setup' )
);

class Pathway

protected static $instance = NULL;

public $plugin_url = '';
private $cpt = 'post'; # Adjust the CPT

public function __construct()

public static function get_instance()

NULL === self::$instance and self::$instance = new self;
return self::$instance;


public function plugin_setup()


$this->plugin_url = '';
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue' ) );

// if (is_admin())
//
// require_once( $this->plugin_url . 'admin/index.php' );
//
// register_activation_hook( __FILE__, 'install' );
//
// return;
//

add_shortcode( 'pathway', array($this, 'shortcode'));

add_action( 'wp_ajax_ajax_login', array( $this, 'ajax_login' ) );
add_action( 'wp_ajax_nopriv_ajax_login', array( $this, 'ajax_login' ) );

add_action( 'wp_ajax_ajax_register', array( $this, 'ajax_register' ) );
add_action( 'wp_ajax_nopriv_ajax_register', array( $this, 'ajax_register' ) );


public function enqueue()

wp_enqueue_script( 'vuejs', 'https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js' );
wp_enqueue_script(
'ajax-handle-form',
"$this->plugin_url/wp-content/plugins/pathway/frontend/js/scripts.js"
);
wp_localize_script(
'ajax-handle-form',
'wp_ajax',
array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'ajaxnonce' => wp_create_nonce( 'ajax_post_validation' )
)
);

public function ajax_login()

echo 'login';exit;

public function ajax_register()

echo 'register';exit;


public function shortcode()

if (!isset($_SESSION['pathway_login']))

self::view('forms/login');




public static function view( $name, array $args = array() )

foreach ( $args AS $key => $val )
$$key = $val;


// $file = $this->plugin_url . 'views/'. $name . '.php';
$file = 'views/'. $name . '.php';

include( $file );





Please correct me if i'm going wrong somewhere, there's so many mixed guides online showing different ways. Within this file i'm basically:



  1. Adding my scripts and assigning the PHP values.

  2. I would be then starting the admin section however has to comment this out for the AJAX call, this is my issue.

  3. Registering my shortcode.

  4. Adding the actions for the AJAX form submit.

Obviously my issue is that when I hit the is_admin from the AJAX call it is returning true, when it should be false as an public visitor can submit this form. The wp_ajax_nopriv action doesn't appear to work which would solve the issue, this is probably due to me being logged into WordPress.



I have tried logging out of WordPress but the is_admin still returns true!



Can someone advise?










share|improve this question






















  • Not sure I completely understand what you're trying to do and what the problem is, but the is_admin() tells you whether you're on an admin page or a frontend page and the wp_ajax_nopriv applies to non-logged in users, not non-admin users. Again, I'm not fully understanding, but you may want to put some conditionals in your methods i.e. if( current_user_can('administrator') )...code...

    – git-e-up
    Nov 12 '18 at 23:00















1















i'm trying to make a plugin for WordPress, which is has got an admin section for some basic settings, and also registers some shortcode to display some HTML, which is basically a form.



Here is my main plugin file, plugins/my-plugin/my-plugin.php:



 /**
* Plugin Name: Pathway
* Plugin URI: http://www.martynleeball.com/
* Description: Pathway integration.
* Version: 1.0
* Author: Martyn Lee Ball
* Author URI: https://www.martynleeball.com/
**/

define('PATHWAY_VERSION', '0.0.8');
define('PATHWAY_AUTHOR', 'Martyn Lee Ball');
define('PATHWAY__MINIMUM_WP_VERSION', '4.*');
define('PATHWAY_CONTACT', 'martynleeball@gmail.com');

add_action(
'plugins_loaded',
array ( Pathway::get_instance(), 'plugin_setup' )
);

class Pathway

protected static $instance = NULL;

public $plugin_url = '';
private $cpt = 'post'; # Adjust the CPT

public function __construct()

public static function get_instance()

NULL === self::$instance and self::$instance = new self;
return self::$instance;


public function plugin_setup()


$this->plugin_url = '';
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue' ) );

// if (is_admin())
//
// require_once( $this->plugin_url . 'admin/index.php' );
//
// register_activation_hook( __FILE__, 'install' );
//
// return;
//

add_shortcode( 'pathway', array($this, 'shortcode'));

add_action( 'wp_ajax_ajax_login', array( $this, 'ajax_login' ) );
add_action( 'wp_ajax_nopriv_ajax_login', array( $this, 'ajax_login' ) );

add_action( 'wp_ajax_ajax_register', array( $this, 'ajax_register' ) );
add_action( 'wp_ajax_nopriv_ajax_register', array( $this, 'ajax_register' ) );


public function enqueue()

wp_enqueue_script( 'vuejs', 'https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js' );
wp_enqueue_script(
'ajax-handle-form',
"$this->plugin_url/wp-content/plugins/pathway/frontend/js/scripts.js"
);
wp_localize_script(
'ajax-handle-form',
'wp_ajax',
array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'ajaxnonce' => wp_create_nonce( 'ajax_post_validation' )
)
);

public function ajax_login()

echo 'login';exit;

public function ajax_register()

echo 'register';exit;


public function shortcode()

if (!isset($_SESSION['pathway_login']))

self::view('forms/login');




public static function view( $name, array $args = array() )

foreach ( $args AS $key => $val )
$$key = $val;


// $file = $this->plugin_url . 'views/'. $name . '.php';
$file = 'views/'. $name . '.php';

include( $file );





Please correct me if i'm going wrong somewhere, there's so many mixed guides online showing different ways. Within this file i'm basically:



  1. Adding my scripts and assigning the PHP values.

  2. I would be then starting the admin section however has to comment this out for the AJAX call, this is my issue.

  3. Registering my shortcode.

  4. Adding the actions for the AJAX form submit.

Obviously my issue is that when I hit the is_admin from the AJAX call it is returning true, when it should be false as an public visitor can submit this form. The wp_ajax_nopriv action doesn't appear to work which would solve the issue, this is probably due to me being logged into WordPress.



I have tried logging out of WordPress but the is_admin still returns true!



Can someone advise?










share|improve this question






















  • Not sure I completely understand what you're trying to do and what the problem is, but the is_admin() tells you whether you're on an admin page or a frontend page and the wp_ajax_nopriv applies to non-logged in users, not non-admin users. Again, I'm not fully understanding, but you may want to put some conditionals in your methods i.e. if( current_user_can('administrator') )...code...

    – git-e-up
    Nov 12 '18 at 23:00













1












1








1








i'm trying to make a plugin for WordPress, which is has got an admin section for some basic settings, and also registers some shortcode to display some HTML, which is basically a form.



Here is my main plugin file, plugins/my-plugin/my-plugin.php:



 /**
* Plugin Name: Pathway
* Plugin URI: http://www.martynleeball.com/
* Description: Pathway integration.
* Version: 1.0
* Author: Martyn Lee Ball
* Author URI: https://www.martynleeball.com/
**/

define('PATHWAY_VERSION', '0.0.8');
define('PATHWAY_AUTHOR', 'Martyn Lee Ball');
define('PATHWAY__MINIMUM_WP_VERSION', '4.*');
define('PATHWAY_CONTACT', 'martynleeball@gmail.com');

add_action(
'plugins_loaded',
array ( Pathway::get_instance(), 'plugin_setup' )
);

class Pathway

protected static $instance = NULL;

public $plugin_url = '';
private $cpt = 'post'; # Adjust the CPT

public function __construct()

public static function get_instance()

NULL === self::$instance and self::$instance = new self;
return self::$instance;


public function plugin_setup()


$this->plugin_url = '';
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue' ) );

// if (is_admin())
//
// require_once( $this->plugin_url . 'admin/index.php' );
//
// register_activation_hook( __FILE__, 'install' );
//
// return;
//

add_shortcode( 'pathway', array($this, 'shortcode'));

add_action( 'wp_ajax_ajax_login', array( $this, 'ajax_login' ) );
add_action( 'wp_ajax_nopriv_ajax_login', array( $this, 'ajax_login' ) );

add_action( 'wp_ajax_ajax_register', array( $this, 'ajax_register' ) );
add_action( 'wp_ajax_nopriv_ajax_register', array( $this, 'ajax_register' ) );


public function enqueue()

wp_enqueue_script( 'vuejs', 'https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js' );
wp_enqueue_script(
'ajax-handle-form',
"$this->plugin_url/wp-content/plugins/pathway/frontend/js/scripts.js"
);
wp_localize_script(
'ajax-handle-form',
'wp_ajax',
array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'ajaxnonce' => wp_create_nonce( 'ajax_post_validation' )
)
);

public function ajax_login()

echo 'login';exit;

public function ajax_register()

echo 'register';exit;


public function shortcode()

if (!isset($_SESSION['pathway_login']))

self::view('forms/login');




public static function view( $name, array $args = array() )

foreach ( $args AS $key => $val )
$$key = $val;


// $file = $this->plugin_url . 'views/'. $name . '.php';
$file = 'views/'. $name . '.php';

include( $file );





Please correct me if i'm going wrong somewhere, there's so many mixed guides online showing different ways. Within this file i'm basically:



  1. Adding my scripts and assigning the PHP values.

  2. I would be then starting the admin section however has to comment this out for the AJAX call, this is my issue.

  3. Registering my shortcode.

  4. Adding the actions for the AJAX form submit.

Obviously my issue is that when I hit the is_admin from the AJAX call it is returning true, when it should be false as an public visitor can submit this form. The wp_ajax_nopriv action doesn't appear to work which would solve the issue, this is probably due to me being logged into WordPress.



I have tried logging out of WordPress but the is_admin still returns true!



Can someone advise?










share|improve this question














i'm trying to make a plugin for WordPress, which is has got an admin section for some basic settings, and also registers some shortcode to display some HTML, which is basically a form.



Here is my main plugin file, plugins/my-plugin/my-plugin.php:



 /**
* Plugin Name: Pathway
* Plugin URI: http://www.martynleeball.com/
* Description: Pathway integration.
* Version: 1.0
* Author: Martyn Lee Ball
* Author URI: https://www.martynleeball.com/
**/

define('PATHWAY_VERSION', '0.0.8');
define('PATHWAY_AUTHOR', 'Martyn Lee Ball');
define('PATHWAY__MINIMUM_WP_VERSION', '4.*');
define('PATHWAY_CONTACT', 'martynleeball@gmail.com');

add_action(
'plugins_loaded',
array ( Pathway::get_instance(), 'plugin_setup' )
);

class Pathway

protected static $instance = NULL;

public $plugin_url = '';
private $cpt = 'post'; # Adjust the CPT

public function __construct()

public static function get_instance()

NULL === self::$instance and self::$instance = new self;
return self::$instance;


public function plugin_setup()


$this->plugin_url = '';
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue' ) );

// if (is_admin())
//
// require_once( $this->plugin_url . 'admin/index.php' );
//
// register_activation_hook( __FILE__, 'install' );
//
// return;
//

add_shortcode( 'pathway', array($this, 'shortcode'));

add_action( 'wp_ajax_ajax_login', array( $this, 'ajax_login' ) );
add_action( 'wp_ajax_nopriv_ajax_login', array( $this, 'ajax_login' ) );

add_action( 'wp_ajax_ajax_register', array( $this, 'ajax_register' ) );
add_action( 'wp_ajax_nopriv_ajax_register', array( $this, 'ajax_register' ) );


public function enqueue()

wp_enqueue_script( 'vuejs', 'https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js' );
wp_enqueue_script(
'ajax-handle-form',
"$this->plugin_url/wp-content/plugins/pathway/frontend/js/scripts.js"
);
wp_localize_script(
'ajax-handle-form',
'wp_ajax',
array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'ajaxnonce' => wp_create_nonce( 'ajax_post_validation' )
)
);

public function ajax_login()

echo 'login';exit;

public function ajax_register()

echo 'register';exit;


public function shortcode()

if (!isset($_SESSION['pathway_login']))

self::view('forms/login');




public static function view( $name, array $args = array() )

foreach ( $args AS $key => $val )
$$key = $val;


// $file = $this->plugin_url . 'views/'. $name . '.php';
$file = 'views/'. $name . '.php';

include( $file );





Please correct me if i'm going wrong somewhere, there's so many mixed guides online showing different ways. Within this file i'm basically:



  1. Adding my scripts and assigning the PHP values.

  2. I would be then starting the admin section however has to comment this out for the AJAX call, this is my issue.

  3. Registering my shortcode.

  4. Adding the actions for the AJAX form submit.

Obviously my issue is that when I hit the is_admin from the AJAX call it is returning true, when it should be false as an public visitor can submit this form. The wp_ajax_nopriv action doesn't appear to work which would solve the issue, this is probably due to me being logged into WordPress.



I have tried logging out of WordPress but the is_admin still returns true!



Can someone advise?







php ajax wordpress






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 22:15









Martyn BallMartyn Ball

1,35522552




1,35522552












  • Not sure I completely understand what you're trying to do and what the problem is, but the is_admin() tells you whether you're on an admin page or a frontend page and the wp_ajax_nopriv applies to non-logged in users, not non-admin users. Again, I'm not fully understanding, but you may want to put some conditionals in your methods i.e. if( current_user_can('administrator') )...code...

    – git-e-up
    Nov 12 '18 at 23:00

















  • Not sure I completely understand what you're trying to do and what the problem is, but the is_admin() tells you whether you're on an admin page or a frontend page and the wp_ajax_nopriv applies to non-logged in users, not non-admin users. Again, I'm not fully understanding, but you may want to put some conditionals in your methods i.e. if( current_user_can('administrator') )...code...

    – git-e-up
    Nov 12 '18 at 23:00
















Not sure I completely understand what you're trying to do and what the problem is, but the is_admin() tells you whether you're on an admin page or a frontend page and the wp_ajax_nopriv applies to non-logged in users, not non-admin users. Again, I'm not fully understanding, but you may want to put some conditionals in your methods i.e. if( current_user_can('administrator') )...code...

– git-e-up
Nov 12 '18 at 23:00





Not sure I completely understand what you're trying to do and what the problem is, but the is_admin() tells you whether you're on an admin page or a frontend page and the wp_ajax_nopriv applies to non-logged in users, not non-admin users. Again, I'm not fully understanding, but you may want to put some conditionals in your methods i.e. if( current_user_can('administrator') )...code...

– git-e-up
Nov 12 '18 at 23:00












1 Answer
1






active

oldest

votes


















0














is_admin will return true on all ajax calls.



It is not actually a useful function to check the user as it checks the uri rather than the user details, i.e. if on a admin page = true, if not false.



Now I was a little confused about your question here, it appears you want the is_admin to return false if its actually an ajax call?



 if ( is_admin() && ! wp_doing_ajax() ) 


It will return false on ajax calls.



If you are checking there is an "admin" logged in, as in can edit posts, see the other capabilities here



 if ( current_user_can( 'edit_post' ) ) 


The no_priv hook will not work when logged in, its not called.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270878%2fwordpress-ajax-is-admin-is-true-causing-issues%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    is_admin will return true on all ajax calls.



    It is not actually a useful function to check the user as it checks the uri rather than the user details, i.e. if on a admin page = true, if not false.



    Now I was a little confused about your question here, it appears you want the is_admin to return false if its actually an ajax call?



     if ( is_admin() && ! wp_doing_ajax() ) 


    It will return false on ajax calls.



    If you are checking there is an "admin" logged in, as in can edit posts, see the other capabilities here



     if ( current_user_can( 'edit_post' ) ) 


    The no_priv hook will not work when logged in, its not called.






    share|improve this answer



























      0














      is_admin will return true on all ajax calls.



      It is not actually a useful function to check the user as it checks the uri rather than the user details, i.e. if on a admin page = true, if not false.



      Now I was a little confused about your question here, it appears you want the is_admin to return false if its actually an ajax call?



       if ( is_admin() && ! wp_doing_ajax() ) 


      It will return false on ajax calls.



      If you are checking there is an "admin" logged in, as in can edit posts, see the other capabilities here



       if ( current_user_can( 'edit_post' ) ) 


      The no_priv hook will not work when logged in, its not called.






      share|improve this answer

























        0












        0








        0







        is_admin will return true on all ajax calls.



        It is not actually a useful function to check the user as it checks the uri rather than the user details, i.e. if on a admin page = true, if not false.



        Now I was a little confused about your question here, it appears you want the is_admin to return false if its actually an ajax call?



         if ( is_admin() && ! wp_doing_ajax() ) 


        It will return false on ajax calls.



        If you are checking there is an "admin" logged in, as in can edit posts, see the other capabilities here



         if ( current_user_can( 'edit_post' ) ) 


        The no_priv hook will not work when logged in, its not called.






        share|improve this answer













        is_admin will return true on all ajax calls.



        It is not actually a useful function to check the user as it checks the uri rather than the user details, i.e. if on a admin page = true, if not false.



        Now I was a little confused about your question here, it appears you want the is_admin to return false if its actually an ajax call?



         if ( is_admin() && ! wp_doing_ajax() ) 


        It will return false on ajax calls.



        If you are checking there is an "admin" logged in, as in can edit posts, see the other capabilities here



         if ( current_user_can( 'edit_post' ) ) 


        The no_priv hook will not work when logged in, its not called.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 1:21









        DavidDavid

        5,13731733




        5,13731733



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270878%2fwordpress-ajax-is-admin-is-true-causing-issues%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo