Blog

  • Add Administration Panel Menu Items in CS-Cart

    I wrote this tutorial using CS-Cart v4.18.4.

    Template to Override

    Copy this file from its location in core.

    design/backend/templates/components/menu/get_primary_items.tpl

    Override Location

    Use a “my_changes” add-on as described in CS-Cart documentation to provide a custom version of the template.

    design/backend/templates/addons/my_changes/overrides/components/menu/get_primary_items.tpl

    Sample Code

    This template file creates the screenshot above.

    {capture name="get_items"}
        {$additional_items = [
    		upload => [
    			title => "Get Breakfast",
    			position => 100,
    			icon => 'icon-inbox',
    			href => "https://breakfastco.xyz/",
    			id_path => "upload",
    			active => true
    		]
    	]}
        {* Create main menu *}
        {$navigation_home = [
            home => [
                title => __("home"),
                position => 10,
                icon => "icon-home",
                href => "index.index",
                id_path => "home",
                active => ($runtime.controller === "index" && $runtime.mode === "index")
            ]
        ]}
    
        {* Get additional menu items: $additional_items *}
        {include file="components/menu/get_additional_items.tpl"}
    
        {if $smarty.const.BLOCK_MANAGER_MODE}
            {$items = $navigation.static.central}
        {else}
            {$items = $navigation_home|array_merge:
                ($navigation.static.central|default:[])|array_merge:
                $additional_items
            }
        {/if}
    
        {* Get block manager data for $items and $attrs_wrapper *}
        {include file="components/menu/get_block_manager_data.tpl"}
    
        {$primary_items = $items scope=parent}
        {$attrs_wrapper = $attrs_wrapper scope=parent}
        {$show_collapse_default = $show_collapse_default scope=parent}
        {$main_menu_primary_class = $main_menu_primary_class scope=parent}
    {/capture}

  • Logged-out Users Run Your WordPress Cron Jobs

    ? Code that works in a plugin might not behave the same in a scheduled wp-cron job.

    Things That Don’t Always Work in WordPress Cron Jobs

    1. get_posts() calls that return posts with unpublished statuses
      Workaround: use $wpdb queries to retrieve posts or force admin
    2. tax_input parameter during wp_insert_post() or wp_update_post() calls
      Workaround: use wp_set_object_terms() or force_admin
    3. wp_get_update_data()
    4. Other functions or codes that call current_user_can() or current_user_can_for_blog()

    Why do wp-cron jobs behave differently?

    WordPress cron jobs are triggered by any visitor to your site on or after the scheduled time. A logged-out visitor could be running your WordPress cron job.

    Any code that uses current_user_can() to restrict permissions can behave differently during wp-cron, including get_posts() and wp_update_post() calls.

    SOLUTIONS

    1. Test Your Code as a Logged-Out Visitor

    Testing wp-cron job code with a tool that runs tasks on demand (like the fantastic Advanced Cron Manager) will fool you into believing the code will always run as an Administrator user. It’s not true. Use a short interval during development so it’s easier to trigger jobs as a logged-out visitor.

    2. Briefly Force Administrator

    Use wp_set_current_user() to explicitly use an Administrator account at the beginning of your job’s code, and restore the original user ID even if zero to avoid leaking Administrator privileges.

    $user_id = get_current_user_id(); // Could be 0.
    wp_set_current_user( 1 ); // One (1) is the user ID of an Administrator.
    
    // Do the stuff that needs to run as an admin.
    $drafts = get_posts( array(
    	'post_status' => 'draft', // Drafts aren't accessible to logged-out users.
    ) );
    
    // Don't leak Administrator privileges, restore the previous user even.
    wp_set_current_user( $user_id );

    2. Use Real Cron

    Almost every hosting company on the planet has written an article about triggering wp-cron with a real cron job. Here’s one at Kinsta.