Unyson icon indicating copy to clipboard operation
Unyson copied to clipboard

Add metabox for page template?

Open osbulbul opened this issue 11 years ago • 19 comments

I didn't see anything for that. So just i want to ask do you have metabox adding feature for page templates or do you have plan for adding that? It is very important thing i think. For example we have 5 different homepage design in our template and all of them have their own properties. When user select Homepage 1 design we have to show design spesific options.

osbulbul avatar Aug 30 '15 11:08 osbulbul

Changing post options depending on Page Template select value, no, we don't have that.

ghost avatar Aug 30 '15 12:08 ghost

Do you think it is unnecessary or something else or you don't have time?

osbulbul avatar Aug 30 '15 19:08 osbulbul

@coderontheroad are you talking about this http://prntscr.com/8aphja

or something else?

danyj avatar Aug 30 '15 19:08 danyj

@danyj yes exactly. For example you have About Us Template and when you choose about us a new metabox shows under the editor. Spesifically for About Us template and it doesn't show any other place.

osbulbul avatar Aug 30 '15 19:08 osbulbul

@coderontheroad I can't do that. Post options is one array that is loaded from a file, it can't be changed depending on some option value.

So from me it's a No for this feature. If you know a good idea how to implement this, then we can inspect it.

ghost avatar Aug 30 '15 20:08 ghost

my bad , ok I see what you mean , you want specific settings depending on the Page template select change , that is another story. I thought you just wanted to see that meta box.

You might get away with http://manual.unyson.io/en/latest/options/built-in-option-types.html#multi-select

in here see video min 3.50 http://manual.unyson.io/en/latest/options/introduction.html#content

danyj avatar Aug 30 '15 20:08 danyj

@moldcraft I don't know right now, but i will research about that. For now i can show just working example. You can see an example in WPAlchemy Metabox Class(http://www.farinspace.com/wpalchemy-metabox/). They have two properties for that: 'exclude_template' is for remove metabox in given page template and 'include_template' is for add metabox just for given page template.

osbulbul avatar Aug 30 '15 20:08 osbulbul

Just for a theory: Maybe we can create/check templates folder in framework-customizations/posts. Then if theme has a page template same name with one of the templates folder files, we can show options only in that page template. I hope i can write my thoughts correctly :)

osbulbul avatar Aug 30 '15 20:08 osbulbul

I will try to explain why it's not easy to make this tomorrow (can't find the words now).

ghost avatar Aug 30 '15 20:08 ghost

@moldcraft okay :)

osbulbul avatar Aug 30 '15 20:08 osbulbul

The options needs to be loaded all, because when you change Template select value some of them will show/hide.

So the only way is to make the show/hide from javascript only. In database will be saved all options and will be no changes in how options works.

To add a special parameter in core for all options to control "For which template this option should be visible" is complicated. Easier would be to create a small functionality for the Templates select (that will not affect anything and can be easy disabled or removed), to specify for each template, which options to be visible, or which options are related to that template and should be hidden if that template is not selected. So:

  • Options that are not specified in any template will be always visible
  • Options that are specified in one or more templates, will be visible only when those templates are selected

Let's say we will specify that in the following format:

array(
    'template/name.php' => array(
        'option_id' => array(
            // a place for future options, if will be needed
            // at the moment just an empty array
        ),
        'option_id' => array(),
        ...
    ),
    'another-template.php' => array(
        'option_id' => array(),
        ...
    ),
)

this array will be sent to javascript. On Templates select change, we need to find all options on the page by 'option_id', this can be done by id

#fw-backend-option-{option_id}

These problems that will happen:

  • When all options in a container are hidden, the container should be also hidden.
    • Then we need to check if that container is inside another container, and if everything inside it is hidden, to hide it too. Continue this until the parent container contains some visible elements or there are no more parents.
    • On option show, we need to do the reverse thing for the parent containers. But also we need to detect if that container is not hidden by other scripts, for e.g. a tab can be hidden, but it's ok, we don't need to show it, because another tab is currently visible. In this can we can use a special css class to hide for e.g. .fw-template-hidden instead of style="display:none;", and just add/remove that class.
  • Currently only multi-picker has the functionality for show/hide options depending on picker choice. And this gave us headaches in past (now it seems to be fixed) because the css is made with rules like :last-child, .first-child .a + .b to make options and separators look good, but when you hide last child or .b, some element remains with inadequate styles that look bad, or orphan separators.

For me this looks complicated and I don't know if it's worth it.

ghost avatar Sep 01 '15 17:09 ghost

Thanks for all the informations. The javascript based solution is not the right way i think. We should think more basic :) I am trying to implement if i can achive something i will give information to you. I am just curious about have you ever need page spesific options or not? if need what are you doing?

osbulbul avatar Sep 01 '15 19:09 osbulbul

@moldcraft i have good news :)

I create base plan for page template spesific options panel. It has bugs but it works. Lets see what I did;

First; (core/components/backend.php) I duplicate this line; add_action('add_meta_boxes', array($this, '_action_create_post_meta_boxes'), 10, 2); to this line; add_action('add_meta_boxes', array($this, '_action_create_page_template_meta_boxes'), 10, 2);

Second; (core/components/backend.php) I duplicate '_action_create_post_meta_boxes' function to '_action_create_page_template_meta_boxes' function. And in new function i change this line; $options = fw()->theme->get_post_options( $post_type ); to this lines;

    $page_template_slug = get_page_template_slug($post->ID);
    if(empty($page_template_slug)){
        return;
    }
    $options = fw()->theme->get_page_template_options( $page_template_slug, $post_type );

Third; (core/components/theme.php) I duplicate 'get_options' function to 'get_pagetemplate_options' function. And i change this line; $path = $this->locate_path('/options/'. $name .'.php'); to this line; $path = $this->locate_path('/options/'. $name);

Fourth; (core/components/theme.php) I duplicate 'get_post_options' function to 'get_page_template_options' function. And new function look like this;

    public function get_page_template_options($page_template_slug, $post_type)
    {
        if($post_type != 'page'){
            return;
        }

        $cache_key = self::$cache_key .'/options/posts/'. $page_template_slug;

        try {
            return FW_Cache::get($cache_key);
        } catch (FW_Cache_Not_Found_Exception $e) {
            $options = apply_filters('fw_post_options', $this->get_pagetemplate_options('posts/'. $page_template_slug), $post_type);

            FW_Cache::set($cache_key, $options);

            return $options;
        }
    }

Result Now i can create a file in framework-customizations/theme/options/posts/ for page templates which file name is same name with page template file name. For example when in theme i have page-full-width.php template file and when i create options file with same name, options shows only when you select page template and save page.

bugs It works you can see the metaboxes and titles. But options not rendering. I don't know why but i can see only empty metaboxes.

So, am I in right way? Whats your opinions?

osbulbul avatar Oct 23 '15 21:10 osbulbul

In regular metaboxes, outside of the unyson framework, we can do it this way. Monitor for select option template change via javascript, and based on that, just remove a set of metaboxes from the DOM, and when the user saves the options, since those metaboxes are not present, there will be no data for them in database. Metaboxes are removed from the DOM , but they are cached so if the user changes the template we put them back in.

Don't know it this can be done with the unyson framework. Probably all options will be saved to the database. But later when you consume the data from the options, you can choose to ignore the data that doesn't belong to the particular template.

I've implemented something like that long time ago....

ivandotv avatar Nov 19 '15 22:11 ivandotv

Something like this should be possible now with fw-storage Custom Save Location.

In theory, create a multi-picker that mirrors the _wp_page_template post option.

// framework-customizations/theme/options/posts/page.php:
$options => array(
    '_wp_page_template' => array(
        'label' => false,
        'type' => 'multi-picker',
        'picker' => array(
            'value' => array(
                'label' => false,
                'type' => 'select',
                'fw-storage' => array(
                    'type' => 'post-meta',
                    'post-meta' => '_wp_page_template',
                ),
                'choices' => array(),
            ),
        ),
        'choices' => array(
            'my-page-template.php' => array(

                // Options for this template only

            ),                                    
        ),
    ),
);

if (is_admin()) {
    $choices = array( '' => __('Default','fw') );
    foreach ( wp_get_theme()->get_page_templates() as $template => $label)
        $choices[$template] = $label;

    $options['_wp_page_template']['value']['choices'] = $choices;
}

Our option rules over the original, so we can hide that one with css and use our picker instead:

// css in page edit screen:
#post-body #page_template { display: none; }
#post-body label[for="page_template"] { display: none; }

Unfortunately, the above example does not work, because fw-storage is in a picker, or some other reason I haven't figured out. It does NOT update the post option correctly :(

It works however in this other case, without the picker:

// framework-customizations/theme/options/posts/page.php:
$options = array(
    '_wp_page_template' => array(
        'label' => false,
        'type' => 'select',
        'fw-storage' => array(
            'type' => 'post-meta',
            'post-meta' => '_wp_page_template',
        ),
        'choices' => array(), 
    ),
);

I suspect there is some js event not triggering fw-storage option updates when they are in a picker, or, something like that. Any ideas?

albertso avatar Dec 19 '16 01:12 albertso

multi-picker value is not a string, it is a serialized array, so can't work.

The only solution is to create your own storage type (described in docs) that will copy a part of multi-picker array value to _wp_page_template post meta.

ghost avatar Dec 20 '16 09:12 ghost

Ok, I will try with a custom storage type then. Thanks @moldcraft !

albertso avatar Jan 02 '17 17:01 albertso

It is a pity that this feature does not work. I, too, experience difficulties because of this. Really want to have such a feature. If possible, implement. Thank you for your work.

Bogdan808 avatar May 24 '17 07:05 Bogdan808

` if ( get_page_template_slug() == 'template-contacts.php' ) {

$options = array(
	'main' => array(
		'title'   => false,
		'type'    => 'box',
		'options' => array(
			fw()->theme->get_options( 'demo-box' ),
		),
	),
);

} ` file - framework-customizations/theme/options/posts/page.php: You can add validation (if ( get_page_template_slug() == 'template-contacts.php' ) )

psics avatar Jun 29 '17 18:06 psics