最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ajax - How to allow to user non logged in WP system upload in media library?

programmeradmin5浏览0评论

I'm stuck right now. For my project, I have to create login not using WP user system but another system.

Now, when with my system API login, I created some cookie but, obviously I didn't also create wp cookie for user logged in. So, I want to allow a user upload with media library of WP (link of WP codex) some files, but it doesn't work.

I have also edited (temporary core wordpress change) admin-ajax.php for forcing it to create post attachment also in the case of user not logged in:

if ( is_user_logged_in() ) {    
    do_action( 'wp_ajax_' . $_REQUEST['action'] );
} else {
   do_action( 'wp_ajax_' . $_REQUEST['action'] );
 }

but async-upload.php responds with 302 and I don't know how to go forward.

UPDATE:
the problem is the same of this user: click here

I tried to use wp_ajax_my_action and wp_ajax_nopriv_myaction but nothing change. My need is allow to not logged user in WP to upload attachment, and assign this attachment with his ID author (for example, 12345), cause each user must see only yours media (i will use pre_get_posts and yet work).

I'm stuck right now. For my project, I have to create login not using WP user system but another system.

Now, when with my system API login, I created some cookie but, obviously I didn't also create wp cookie for user logged in. So, I want to allow a user upload with media library of WP (link of WP codex) some files, but it doesn't work.

I have also edited (temporary core wordpress change) admin-ajax.php for forcing it to create post attachment also in the case of user not logged in:

if ( is_user_logged_in() ) {    
    do_action( 'wp_ajax_' . $_REQUEST['action'] );
} else {
   do_action( 'wp_ajax_' . $_REQUEST['action'] );
 }

but async-upload.php responds with 302 and I don't know how to go forward.

UPDATE:
the problem is the same of this user: click here

I tried to use wp_ajax_my_action and wp_ajax_nopriv_myaction but nothing change. My need is allow to not logged user in WP to upload attachment, and assign this attachment with his ID author (for example, 12345), cause each user must see only yours media (i will use pre_get_posts and yet work).

Share Improve this question edited Jun 12, 2017 at 13:08 ClodClod91 asked Jun 9, 2017 at 13:58 ClodClod91ClodClod91 1438 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

First, you do not want to edit core WP files, because your changes will disappear when there's an update. That's why you are encouraged to create child themes.

Second, you may want to peruse this Codex page on AJAX. Note that it shows how to handle both kinds of users (logged in and not logged in):

add_action( 'wp_ajax_my_action', 'my_action' );
add_action( 'wp_ajax_nopriv_my_action', 'my_action' );

The my_action function would go in your theme files (when I was working on mine, I created my own plugin to handle AJAX).

Hope this starts you down the right path.

I was facing the same problem and finally got a solution.. As non login user don't have any role.. If we make a user role and assign a role to the every visitor we can solve the problem of uploading files as uploading file is a capability we can add to user role...

I tried the following...

add_role( 'frontend_editor','Front-End Editor', array( 'upload_files' => true) );

if ( is_user_logged_in() ) {    
    $current_user = wp_get_current_user();
    $current_user->add_role('frontend_editor');
}

put it function.php its work for me.

发布评论

评论列表(0)

  1. 暂无评论