ThemeFuse / Unyson

A WordPress framework that facilitates the development of WP themes
http://unyson.io
926 stars 220 forks source link

Getting Post options in WP Editor for a custom post type #3874

Open john-ampwebdesign opened 4 years ago

john-ampwebdesign commented 4 years ago

What I am trying to do is have some options show in the WordPress editor for a custom post type that has been created. The options are based on the items 1/2 way down this page: http://manual.unyson.io/en/latest/options/introduction.html#content Options Files: Post Add/Edit Page: Loads the options from {theme}/framework-customizations/theme/options/posts/{$post_type}.php

Now this works great for a custom post type named "Test" and I create a file called "test.php" and add it to the /framework-customizations/theme/options/posts/ directory. I open the custom post type in the editor and I see my items that are in the test.php file. For example I have a Call To Action Image, URL and button that can be added to the custom post type post/page. This all work great.

But the theme we have created is being used for multiple clients and they each could require a different custom post type name that is created in real time by the client (so we don't need to do it for them). For instance "Music", "Teams", "Portfolio". This is all coded so that they use a single custom template called single-altposttype.php, archive-altposttype.php, search-altposttype.php, etc. No need for each individual custom post type template file.

Those templates work great and it all works on the front end and back end.

The problem is that the Options File does not load to the WordPress editor. It doesn't load the options until I create a file that matches the custom post type slug, ie: music.php, teams.php, etc. It also does not load the post.php options file which loads to the Wordpress editor for Posts.

I have tried an altposttype.php named options file. I have tried the file in the taxonomies folder as well. I have tried it in the parent folder /options/. I have tried a variety of names like custom_post, custom_post_type as well. The only thing that works is the actual "custom post slug".php.

I have tried to look for where this {$post_type}.php (as above on the linked page) is declared, and if I can manipulate it to set it to 'altposttype.php' all the time, it would work for me.

One option is that I was going to have the file created with the new name each time based on the original options file as a template. This is however in a multisite environment and it failed due to write permissions of the client (even as admin, none will be super-admin) and folder permissions. Worked locally on a single site but not on the server.

If I figure this out, I will edit my post to help others who may have the same or similar issue.

Thank you for any help in advance.

Niloys7 commented 4 years ago

Hi, so you like to use same options for "Music, Teams, Portfolio" without creating individual post_type.php under the post folder? well, if so then you can use this code below:

add_filter('fw_post_options', '_filter_theme_fw_post_options', 10, 2); function _filter_theme_fw_post_options($options, $post_type) { if ($post_type == 'music' || $post_type == 'teams' || $post_type == 'portfolio' ) { $options['extra-option'] = array('type' => 'text'); } return $options; }

Sorry if I don't get your question :(

john-ampwebdesign commented 4 years ago

Thank you Niloys7.

This put me on the right track to register the side editor options. Instead of the $options['extra-option'] = array..., I used my $options array. The 'extra-options' put a text input box on the bottom of the edit page. Close but wanted my items on the side.

I got it to work by just taking the $options array from my altposttype.php and pasting it there instead. Therefore I do not need an extra custom post file anymore with the name of the clients custom posts or a generic altposttype one.

This is my final code with some of it edited out for brevity and security:

$post_type = get_query_var('post_type'); function _filter_theme_fw_post_options($options, $post_type) { $cptslug = getcptslug($altposttype); if ($post_type == $cptslug ) { $options = array( 'cpt_side' => array( 'title' => __( 'Header Image', 'k-flow' ), 'type' => 'box', 'context' => 'side', 'priority' => 'low', 'options' => array( 'header_image' => array( ... ), 'additional_content' => array( 'label' => __( 'Additional Content Area', 'k-flow' ), 'value' => '', 'type' => 'wp-editor', ) ), ) ) ); } return $options; }

Niloys7 commented 4 years ago

okay can you please send a screenshot of the section you want to remove. also let me know the code path for that section.

On Fri, Nov 15, 2019, 5:49 AM John Laprairie notifications@github.com wrote:

Thank you Niloys7.

This put me on the right track to register the side editor options. Instead of the $options['extra-option'] = array..., I used my $options array. The 'extra-options' put a text input box on the bottom of the edit page. Close but wanted my items on the side.

I got it to work by just taking the $options array from my altposttype.php and pasting it there instead. Therefore I do not need an extra custom post file anymore with the name of the clients custom posts or a generic altposttype one.

This is my final code with some of it edited out for brevity and security:

` $post_type = get_query_var('post_type'); function _filter_theme_fw_post_options($options, $post_type) { $cptslug = getcptslug($altposttype); if ($post_type == $cptslug ) {

$options = array( 'cpt_side' => array( 'title' => __( 'Header Image', 'k-flow' ), 'type' => 'box', 'context' => 'side', 'priority' => 'low', 'options' => array( 'header_image' => array(

...... ), 'additional_content' => array( 'label' => __( 'Additional Content Area', 'k-flow' ), 'value' => '', 'type' => 'wp-editor', ) ), ) ) );

} return $options;

}

`

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ThemeFuse/Unyson/issues/3874?email_source=notifications&email_token=ABQSNNYVNN2KSQF4NTNVXVTQTXPZBA5CNFSM4JJNMLJKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEEDX3WQ#issuecomment-554139098, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABQSNN6OT7CSRVX6TFVSZN3QTXPZBANCNFSM4JJNMLJA .