Support » Plugin: Elementor Page Builder » Option to enable by default Elementor for custom post type.

  • Hello,

    i am working on a wp theme and i am trying to figure how to set Elementor to be enabled by default for custom post types (for example ‘portfolio’), when the theme is installed.

    Thank You in advance!

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Support suchielementor

    (@suchielementor)

    @mthemesnet HI, In the Elementor dashboard, you can set which post types will be editable in Elementor.

    http://prntscr.com/kpzwfs

    @suchielementor Thank you for your reply. I was aware of the option in dashboard but i was looking for something else.

    I need a PHP hook or something (update_option, etc) in my theme config so when my clients will install the theme, the cpt ‘potrfolio’ will be already checked as editable with Elementor.

    Thanks!

    pingram

    (@pingram3541)

    Here’s how I’d go about it.

    1. Inspect the Elementor Settings “Post Types” area and note the form check field’s use the name attr “elementor_cpt_support[]” – this is the first clue

    2. Search up the plugin and see if I can find that anywhere, most likely a wpdb->options setting somewhere. (wpdb->options = wp_options by default)

    3. Yep, finding it in /plugins/elementor/includes/plugin.php, a check for this starting at line 134 with the default being an object with 2 values of “post” and “page” – if no wp_options table entry exists it falls back to these 2 types.

    4. In that function, we can see it loops through these object values and registers the post types using WordPress’s “add_post_type_support” to the “elementor” feature.

    5. In my theme’s activation hook I would then do the same, check the db for existence of “elementor_cpt_support” and if so, append the “portfolio” post type to the existing option object or simply write a new object to the db [ ‘page’, ‘post’, portfolio’ ].

    See – https://developer.wordpress.org/reference/functions/add_post_type_support/

    Best of luck to you.

    @pingram3541 Hello, and THANK YOU very much for your time and for such a good explaining.
    Unfortunately i tryed for the last 2 hours to make it work but i can`t find the right code for it.

     function mytheme_add_cpt_support() {
    		$cpt_support = get_option( 'elementor_cpt_support', [ 'page', 'post', 'portfolio' ] );
    
    		foreach ( $cpt_support as $cpt_slug ) {
    			add_post_type_support( $cpt_slug, 'elementor' );
    		}
    }
    add_action( 'after_switch_theme', 'mytheme_add_cpt_support' );

    It will be highly appreciated if you can point me the correct code.

    Thank you again in advance!

    • This reply was modified 1 year, 2 months ago by mthemesnet.
    pingram

    (@pingram3541)

    Ok, a few things I see here.

    Your function is a copy of the original hook which still exists and still fires on every “init” (every page load, front or back).

    Yours only fires when switching to your theme but never after that.

    Also that function doesn’t write to the database, it checks for values and if it doesn’t find any under “elementor_cpt_support” it will default to using [ ‘page’, ‘post’, ‘portfolio’ ] as the value for $cpt_support and then loop through each of the types via “add_post_type_support”…but again, the issue is it only applies in your theme’s activation instance and not every time moving forward which explains why it’s not working when you navigate to a portfolio post type – you’re no longer activating the theme and your function doesn’t run.

    The Elementor function continues to be used and doesn’t see “elementor_cpt_support” in the database so it supports only the 2 types which it defines as default. (back to square one)

    This is fine though…because you want to use Elementor’s original check against the db anyway, only after your theme has prepared it.

    You basically want to check if the db options exists, if it doesn’t, write your defaults, if it does, check to see if it already has your post type and if it doesn’t, update it…otherwise you’re good to go.

    function mytheme_add_cpt_support() {
        
        //if exists, assign to $cpt_support var
    	$cpt_support = get_option( 'elementor_cpt_support' );
    	
    	//check if option DOESN'T exist in db
    	if( ! $cpt_support ) {
    	    $cpt_support = [ 'page', 'post', 'portfolio' ]; //create array of our default supported post types
    	    update_option( 'elementor_cpt_support', $cpt_support ); //write it to the database
    	}
    	
    	//if it DOES exist, but portfolio is NOT defined
    	else if( ! in_array( 'portfolio', $cpt_support ) ) {
    	    $cpt_support[] = 'portfolio'; //append to array
    	    update_option( 'elementor_cpt_support', $cpt_support ); //update database
    	}
    	
    	//otherwise do nothing, portfolio already exists in elementor_cpt_support option
    }
    add_action( 'after_switch_theme', 'mytheme_add_cpt_support' );

    @pingram3541
    Give this man a medal!
    Not only that you provided the code for me but you also explained the whole situation point by point. Amazing!

    Thank you for your time Philip!

    pingram

    (@pingram3541)

    Happy to help, happy coding!

    christopher.amirian

    (@christopheramirian)

    I should also thank @pingram3541 with a great piece of code.

    You did it man!

    Thank you @pingram3541,

    Your code perfectly works for me.

    Cheers..! 🙂

    Awesome, glad to help!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Option to enable by default Elementor for custom post type.’ is closed to new replies.