Forums

How do you check whether a request is from the admin panel? (2 posts)

  1. laqrhead
    Member
    Posted 3 years ago #

    I'm filtering some posts, and it's working fine in the from end, but it's also filtering the posts in the admin, which I don't want.

    Here is some example code of what I'm trying to do, and the ??? is where I'm stuck

    add_filter('the_posts', 'my_function' , 1 );
    
    function my_function( $posts ) {
    
        //
        // Check to see if we are logged into the admin panel
        //
        if (???) {
            return $posts;
        }
    
        //
        // If we are not in the admin, filter the posts
        //
    
        // create an array to hold the posts we want to show
        $new_posts = array();
    
        //
        // loop through all the post objects
        //
        foreach( $posts as $post ) {
    
            $include = true;
    
            //
            // Perform Filter comparison
            //
            if ($filterPost>=1) {
                $include = false;
            }
    
            //
            // if we want to include it then
            // add the post object to the new posts array
            //
            if ( $include === true ) {
                $new_posts[] = $post;
            }
        }
    
        //
        // send the new post array back to be used by WordPress
        //
    	return $new_posts;
    }
  2. laqrhead
    Member
    Posted 3 years ago #

    Well, I've come up with a dirty hack, but maybe it's the only way? I'm checking for the wp-admin folder in the URL. I don't know why someone would change the admin folder, but if they did, this wouldn't work.

    if (stristr($_SERVER['SCRIPT_URI'], 'wp-admin') !== FALSE) {
        return $posts;
    }

    If anyone knows of a better way please let me know.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.