get_page_templates example

/** * Gets the page templates available in this theme. * * @since 1.5.0 * @since 4.7.0 Added the `$post_type` parameter. * * @param WP_Post|null $post Optional. The post being edited, provided for context. * @param string $post_type Optional. Post type to get the templates for. Default 'page'. * @return string[] Array of template file names keyed by the template header name. */
function get_page_templates( $post = null, $post_type = 'page' ) {
    return array_flip( wp_get_theme()->get_page_templates( $post$post_type ) );
}

/** * Tidies a filename for url display by the theme file editor. * * @since 2.9.0 * @access private * * @param string $fullpath Full path to the theme file * @param string $containingfolder Path of the theme parent folder * @return string */
'wp-editor',
    sprintf( 'var _wpMetaBoxUrl = %s;', wp_json_encode( $meta_box_url ) ),
    'before'
);

/* * Get all available templates for the post/page attributes meta-box. * The "Default template" array element should only be added if the array is * not empty so we do not trigger the template select element without any options * besides the default value. */
$available_templates = wp_get_theme()->get_page_templates( get_post( $post->ID ) );
$available_templates = ! empty( $available_templates ) ? array_replace(
    array(
        /** This filter is documented in wp-admin/includes/meta-boxes.php */
        '' => apply_filters( 'default_page_template_title', __( 'Default template' ), 'rest-api' ),
    ),
    $available_templates
) : $available_templates;

// Lock settings. $user_id = wp_check_post_lock( $post->ID );
if ( $user_id ) {
    

        $dropdown_args = apply_filters( 'page_attributes_dropdown_pages_args', $dropdown_args$post );
        $pages         = wp_dropdown_pages( $dropdown_args );
        if ( ! empty( $pages ) ) :
            ?> <p class="post-attributes-label-wrapper parent-id-label-wrapper"><label class="post-attributes-label" for="parent_id"><?php _e( 'Parent' ); ?></label></p> <?php echo $pages; ?> <?php         endif; // End empty pages check.     endif;  // End hierarchical check.
    if ( count( get_page_templates( $post ) ) > 0 && get_option( 'page_for_posts' ) != $post->ID ) :
        $template = ! empty( $post->page_template ) ? $post->page_template : false;
        ?> <p class="post-attributes-label-wrapper page-template-label-wrapper"><label class="post-attributes-label" for="page_template"><?php _e( 'Template' ); ?></label> <?php         /** * Fires immediately after the label inside the 'Template' section * of the 'Page Attributes' meta box. * * @since 4.4.0 * * @param string|false $template The template used for the current post. * @param WP_Post $post The current post. */
 else {
            $post             = null;
            $current_template = '';
        }

        // Always allow for updating a post to the same template, even if that template is no longer supported.         if ( $template === $current_template ) {
            return true;
        }

        // If this is a create request, get_post() will return null and wp theme will fallback to the passed post type.         $allowed_templates = wp_get_theme()->get_page_templates( $post$this->post_type );

        if ( isset( $allowed_templates[ $template ] ) ) {
            return true;
        }

        return new WP_Error(
            'rest_invalid_param',
            /* translators: 1: Parameter, 2: List of valid values. */
            sprintf( __( '%1$s is not one of %2$s.' ), 'template', implode( ', ', array_keys( $allowed_templates ) ) )
        );
    }

    
/** * Prints out option HTML elements for the page templates drop-down. * * @since 1.5.0 * @since 4.7.0 Added the `$post_type` parameter. * * @param string $default_template Optional. The template file name. Default empty. * @param string $post_type Optional. Post type to get templates for. Default 'post'. */
function page_template_dropdown( $default_template = '', $post_type = 'page' ) {
    $templates = get_page_templates( null, $post_type );

    ksort( $templates );

    foreach ( array_keys( $templates ) as $template ) {
        $selected = selected( $default_template$templates[ $template ], false );
        echo "\n\t<option value='" . esc_attr( $templates[ $template ] ) . "' $selected>" . esc_html( $template ) . '</option>';
    }
}

/** * Prints out option HTML elements for the page parents drop-down. * * @since 1.5.0 * @since 4.4.0 `$post` argument was added. * * @global wpdb $wpdb WordPress database abstraction object. * * @param int $default_page Optional. The default page ID to be pre-selected. Default 0. * @param int $parent_page Optional. The parent page ID. Default 0. * @param int $level Optional. Page depth level. Default 0. * @param int|WP_Post $post Post ID or WP_Post object. * @return void|false Void on success, false if the page has no children. */
set_post_thumbnail( $post_id$thumbnail_id );
            }
        }
    }

    clean_post_cache( $post_id );

    $post = get_post( $post_id );

    if ( ! empty( $postarr['page_template'] ) ) {
        $post->page_template = $postarr['page_template'];
        $page_templates      = wp_get_theme()->get_page_templates( $post );

        if ( 'default' !== $postarr['page_template'] && ! isset( $page_templates[ $postarr['page_template'] ] ) ) {
            if ( $wp_error ) {
                return new WP_Error( 'invalid_page_template', __( 'Invalid page template.' ) );
            }

            update_post_meta( $post_id, '_wp_page_template', 'default' );
        } else {
            update_post_meta( $post_id, '_wp_page_template', $postarr['page_template'] );
        }
    }

    
$password = $args[2];

        $user = $this->login( $username$password );
        if ( ! $user ) {
            return $this->error;
        }

        if ( ! current_user_can( 'edit_pages' ) ) {
            return new IXR_Error( 403, __( 'Sorry, you are not allowed to access details about this site.' ) );
        }

        $templates            = get_page_templates();
        $templates['Default'] = 'default';

        return $templates;
    }

    /** * Retrieves blog options. * * @since 2.6.0 * * @param array $args { * Method arguments. Note: arguments must be ordered as documented. * * @type int $0 Blog ID (unused). * @type string $1 Username. * @type string $2 Password. * @type array $3 Optional. Options. * } * @return array|IXR_Error */
<?php if ( ! $bulk ) : ?> <label> <span class="title"><?php _e( 'Order' ); ?></span> <span class="input-text-wrap"><input type="text" name="menu_order" class="inline-edit-menu-order-input" value="<?php echo $post->menu_order; ?>" /></span> </label> <?php endif; // ! $bulk ?> <?php endif; // post_type_supports( ... 'page-attributes' ) ?> <?php if ( 0 < count( get_page_templates( null, $screen->post_type ) ) ) : ?> <label> <span class="title"><?php _e( 'Template' ); ?></span> <select name="page_template"> <?php if ( $bulk ) : ?> <option value="-1"><?php _e( '&mdash; No Change &mdash;' ); ?></option> <?php endif; // $bulk ?> <?php                             /** This filter is documented in wp-admin/includes/meta-boxes.php */
                            $default_title = apply_filters( 'default_page_template_title', __( 'Default template' ), 'quick-edit' );
                            ?>
Home | Imprint | This part of the site doesn't use cookies.