0

I use template_lock to lock some Gutenberg blocks. But I would like to use it in three differents post type.

get_post_type_object(); admit only string, no array.

How to use several post type without repeat three times the same function please ?

function wp_template_lock_subject_imposed() {
    $post_type_object = get_post_type_object( 'subject-imposed' );

    $post_type_object->template = array(
        array( 'acf/text-introduction'),
        array( 'acf/text-paragraph')
    );

    $post_type_object->template_lock = false;
}

add_action( 'init', 'wp_template_lock_subject_imposed' );

function wp_template_lock_subject_free() {
    $post_type_object = get_post_type_object( 'subject-free' );

    $post_type_object->template = array(
        array( 'acf/text-introduction'),
        array( 'acf/text-paragraph')
    );

    $post_type_object->template_lock = false;
}

add_action( 'init', 'wp_template_lock_subject_free' );

function wp_template_lock_dissertation() {
    $post_type_object = get_post_type_object( 'dissertation' );

    $post_type_object->template = array(
        array( 'acf/text-introduction'),
        array( 'acf/text-paragraph')
    );

    $post_type_object->template_lock = false;
}

add_action( 'init', 'wp_template_lock_dissertation' );
0

This is more of a PHP question than a WordPress one but you could do the following:

function wp_template_lock_example() {
    // Make an array of your post types
    $post_types = array('subject-imposed', 'subject-free', 'dissertation');

    // Loop the array and apply the template details to each.
    foreach ( $post_types as $post_type ) {
        $post_type_object = get_post_type_object( $post_type );
        $post_type_object->template = array(
            array( 'acf/text-introduction'),
            array( 'acf/text-paragraph')
        );

       $post_type_object->template_lock = false;
    }
}
add_action( 'init', 'wp_template_lock_example' );

Hope it helps!

| improve this answer | |
  • I didn't know how to put my post types in an array. Now I understand. It works perfectly, thank you for your help. – Jandon May 27 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.