0

I am trying to output list by a condition.
I am trying to create a plugin and the plugin addon. when the plugin addon activated, the condition should be fired.

Condition
1. if admin or editor or author, output all of post.
2. if it's not one of them, output only my post ( it's by user id)
However, it should use a hook.

This is original page.

      $lava_listing_posts_args  =
        Array(
            'post_type'         => $post_type
            , 'post_status'     => 'publish'
            , 'posts_per_page'  => 10
            , 'paged'           => $this_paged
            , 'orderby'         => 'modified'           
            , 'tax_query'       => Array()          
        );

        /* Get Current logged in user info */
        $user = wp_get_current_user();
        $allowed_roles = array('editor', 'administrator', 'author');
        if( !array_intersect($allowed_roles, $user->roles ) ) {
            $author_check= array('author'=> $get_current_user_id);
            $lava_listing_posts_args['author'] = $author_check;
            $lava_listing_posts_args = array_merge($lava_listing_posts_args, $author_check);
        }

It works fine using array_merge to add "by author id" when it's a normal user.

So I added the condition code in a hook.
http://prntscr.com/bodiim ( added "do_action") : Original plugin

 $lava_listing_posts_args   =
        Array(
            'post_type'         => $post_type
            , 'post_status'     => 'publish'
            , 'posts_per_page'  => 10
            , 'paged'           => $this_paged
            , 'orderby'         => 'modified'           
            , 'tax_query'       => Array()          
        );

        /* Added them to a hook "lava_lv_support_list_output_restrict" */
        /*$user = wp_get_current_user();
        $allowed_roles = array('editor', 'administrator', 'author');
        if( !array_intersect($allowed_roles, $user->roles ) ) {
            $author_check= array('author'=> $get_current_user_id);
            $lava_listing_posts_args['author'] = $author_check;
            $lava_listing_posts_args = array_merge($lava_listing_posts_args, $author_check);
        }*/

do_action( 'lava_lv_support_list_output_restrict');

http://prntscr.com/bodjlz ( added "add_action" on another include page) : addon plugin

  /*
Hook for output only my tickets for non staff (normal users)
*/

add_action( "lava_lv_support_list_output_restrict", Array( $this, 'lava_list_output_restrict' ) ); 
  /** this is __construct(). I just put here to show you. **/

public function lava_list_output_restrict(){
    $user = wp_get_current_user();
    $allowed_roles = array('editor', 'administrator', 'author');
    if( !array_intersect($allowed_roles, $user->roles ) ) {
        $get_current_user_id = get_current_user_id( );
        $author_check= array('author'=> $get_current_user_id);
        $lava_listing_posts_args['author'] = $author_check;
        $lava_listing_posts_args = array_merge($lava_listing_posts_args, $author_check);
    }       
}

But it doesn't seem to be working. I tried to add whole array ($lava_listing_posts_args). it doesn't call the array.

I guess it doesn't get them if I put the condition in a hook.
How can we make it work?

The hook has to be on an addon plugin (so different page) and when the addon activated, the condition should be fired.

  • 2
    please add all relevant code to the question. No one is likely to follow external links just to understand what is it that you want – Mark Kaplun Jul 4 '16 at 5:56
  • Apologize for the issue. I thought it would better you to understand with screenshots. I have added the detail code. if your time is allowed, please give a line. – pulla Jul 4 '16 at 8:34
  • Are you trying to access the $lava_listing_posts_args array in a different file (and by the looks of it a different class)? – Cai Jul 4 '16 at 8:38
  • @Cai Yes! as you see it works fine at 1st code. but the if condition and array merge should not be there. it should be fired when an addon activated. I believe the code should be working on the addon core page (class). am I doing wrong? – pulla Jul 4 '16 at 8:50
1

It looks like you are trying to access the $lava_listing_posts_args array from a different file (and class). You would need to pass it as an argument of your action.

Although you could do this with actions since you are simply modifying the array you should probably use filters (actions and filters are essentially the same thing, just used differently)

Instead of calling do_action, filter the array like this:

$lava_listing_posts_args = apply_filters( 'lava_lv_support_list_output_restrict', $lava_listing_posts_args );

Then in your addon class, hook on to your filter like this:

add_filter( 'lava_lv_support_list_output_restrict', Array( $this, 'lava_list_output_restrict' ) );

Pass the array to your function as an argument like this (and return the array once you're finished with it):

public function lava_list_output_restrict( $lava_listing_posts_args ) {

    // Do stuff with $lava_listing_posts_args
    return $lava_listing_posts_args;
}

As as aside, I'm not sure exactly what you're trying to do with this code:

if( !array_intersect($allowed_roles, $user->roles ) ) {
    $get_current_user_id = get_current_user_id( );
    $author_check= array('author'=> $get_current_user_id);
    $lava_listing_posts_args['author'] = $author_check;
    $lava_listing_posts_args = array_merge($lava_listing_posts_args, $author_check);
}

It seems to me most of that is unnecessary and all you are really doing is:

if( !array_intersect( $allowed_roles, $user->roles ) ) {
    $lava_listing_posts_args['author'] = get_current_user_id();
}   
  • I am not sure how much I appreciate for your code. it works very fine. – pulla Jul 4 '16 at 9:26
  • The last code is for checking.. roles (logged in user currently). if user role is ( editor, admin, author), then output all of posts. $allowed_roles = array('editor', 'administrator', 'author');).. THANK YOU AGAIN. this is my 1st plugin and good to learn from you about add_filter. !! – pulla Jul 4 '16 at 9:28
  • No problem, glad to help :) If all you're doing in that last code is limiting the posts to the current user then you only need the one line I added, the rest is unnecessary. – Cai Jul 4 '16 at 9:36
  • Oh.. so I don't need to check "editor" and "admin"? thank you again! – pulla Jul 4 '16 at 10:37
  • You check those roles by doing if( !array_intersect($allowed_roles, $user->roles ) ). Which is fine. It's the code inside that if statement that you don't need... – Cai Jul 4 '16 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.