Sorry, you are not allowed to access this page after wp-admin login

If you see the following text, Sorry, you are not allowed to access this page. after logging into WP admin.
Screen Shot 2017-08-27 at 17.30.52

Then you might have change the database prefix. Usermeta table contains
bz_capabilities
bz_user_level
bz_dashboard_quick_press_last_post_id

Where the prefix bz_ is the database prefix specified in wp-config.php

Proof from the source code at https://github.com/WordPress/WordPress/blob/dd46042e3c3b43c5ee3c17862213b0280fb5abfd/wp-includes/class-wp-user.php#L787

Advertisements

WooCommerce check if product in cart is in stock

Check if product in cart is in stock:

foreach (WC()->cart->cart_contents as $item) {
    $product_id = isset($item['variation_id']) ? $item['variation_id'] : $item['product_id'];
    $product = new \WC_Product_Factory();
    $product = $product->get_product($product_id);
    if ($item['quantity'] > $product->get_stock_quantity()) {
        $inStock = false;
        break;
    }
}

WordPress custom search for custom post type

After creating a custom post type for an Inventory plugin the client needed also customized search for the columns.

Custom search for custom post type

First the pre_get_posts looked as a good candidate for hooking, but ended up with filtering via posts_clauses_request

add_filter('posts_clauses_request', 'customSearchClause');
function customSearchClause($query)
{
    global $wp_query;

    if (!is_search()) {
        return $query;
    }

    $term = $wp_query->query['s'];
    $query['join'] = 'INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)';
    $query['where'] = "AND wp_posts.post_type = '$this->post_type'
    AND (
        (wp_posts.post_title LIKE '%{$term}%')
        OR (wp_postmeta.meta_key = 'product' AND wp_postmeta.meta_value LIKE '%{$term}%')
        OR (wp_postmeta.meta_key = 'product_code' AND wp_postmeta.meta_value LIKE '%{$term}%')
    )";

    return $query;
}

Use WordPress core as PHP framework

For a task I required using headless WordPress, just the core without a template. Using as a PHP framework. More info about integration is available at http://codex.wordpress.org/Integrating_Wordpress_with_Your_Website

Power tip:
To avoid plugins redirecting after requiring wp-blog-header.php define DOING_CRON const or might be DOING_AJAX or WP_ADMIN. Gist snippet. You could also check defined constants. To avoid redirect you also might redefine wp_redirect() function.

 

<?php

define('DOING_CRON', true);
require dirname(__FILE__).'/wp-blog-header.php';

// Your code goes here...