How do I check if a theme has a file single-cpt.php?

I’m creating a cpt plugin that will add ‘single-cpt.php’ in the theme directory.
The following code works but only if I put it in a functions.php (theme) folder.

// check if the file exists
$checkFile = locate_template('single-cpt.php');
if (!empty($checkFile)) {
    echo "Found!";
} else {
    echo "Not found";
}

How do I get the plugin check if the current theme has that single-cpt.php file ?

Go to Source
Author: Ash

How to change the Media Uploader screen from WordPress admin to normal browse screen for frontend users for featured image submission

I have a theme form in my frontend which takes the featured image from the user. But my problem is when the user clicks the upload button they are taken to the normal MEDIA UPLOADER screen which is normal for admin users. On top of that, the site visitors can also see the other media library images. I want to change this media uploader screen to just file browse screen. My site users are of “Authors” category. Please need help!

Go to Source
Author: Muhammad Aurangzeb Khan

Settings checkbox not triggering function

I have recently created this code as a plugin to store a setting in the WooCommerce general settings. The checkbox is meant to

// Check if WooCommerce is active
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {

    // Settings
    function hm_woo_disable_shop_settings( $settings ) {
        
        $settings[] = array(
            'title' => __( 'Disable Shop', 'wcds' ),
            'type' => 'title',
            'desc' => 'Check this box to disable all WooCommerce features',
            'id' => 'wcds_settings',
        );

        // Checkbox
        $settings[] = array(
            'title'             => __( 'Disable', 'woocommerce' ),
            'desc'              => __( 'This has no affect to the database and will only limit frontend functions', 'wcds' ),
            'id'                => 'wcds_checkbox',
            'default'           => '',
            'type'              => 'checkbox',
            'desc_tip'          => true,
            'value'             => get_option('wcds_checkbox'),
            'checked'           => esc_attr( checked( 1, get_option('wcds_checkbox'), false ) )
        );

        $settings[] = array( 'type' => 'sectionend', 'id' => 'wcds_settings' );
        return $settings;
    }
    add_filter( 'woocommerce_general_settings','hm_woo_disable_shop_settings', 10, 2 );
    

    // Functions
    function woocommerce_disable_shop() {
        
        global $woocommerce;

        // Get the checkbox value from settings
        if( get_option( 'wcds_checkbox' ) === '1' ) {
            add_filter( 'woocommerce_is_purchasable', '__return_false');
        }
        
    }
    add_action( 'init', 'woocommerce_disable_shop' );
    
}

Go to Source
Author: Jesse

publish_{$post_type} hook works only for posts and not for custom post types or pages

I’m trying to send push notification when any of posts, custom post types or pages is published. I’m getting enabled post types from the plugin settings and adding action via foreach loop in my class __construct method. The problem is that it only works for posts and not for any of custom post types or pages. Here is my function and action:

foreach ((array)get_option('PushPostTypes') as $postType) {
    add_action("publish_{$postType}",  array($this, 'doNewPostPush'), 10, 2);
}

public function doNewPostPush($id, $post) {
    $pushData = array(
        'title' => $post->post_title,
        'body' => strip_tags($post->post_content),
        'data' => array(
            'url' => trailingslashit(get_permalink($id)),
        ),
    );

    if (has_post_thumbnail($id)) {
        $pushData['image'] = get_the_post_thumbnail_url($id);
    }
    
    $this->sendNotification($pushData);
}

get_option('PushPostTypes') is an array of post types that user choose, for example: array('post', 'page', 'custom_post');

Any idea why it only works for post and not for pages or custom post types?

Go to Source
Author: Akaki Khujadze