admin管理员组文章数量:1327849
I have a function that I re-use in a number of custom themes when required to fulfil a customers need. I understand this can be achieved now with repeaters in ACF Pro but as I had a working function, I chose to use this.
The problem I'm now facing is I'm really stuck with getting this to show on a custom post type within v5.4.2. I even installed the 'classic Editor' plugin to see if it was an issue with legacy meta fields but no joy...
The code of the function with comments is:
//custom post type attachments for bikes.
add_filter( 'attachments_default_instance', '__return_false' ); // disable the default instance
function post_attachments( $attachments )
{
$fields = array(
array(
'name' => 'title', // unique field name
'type' => 'text', // registered field type
'label' => __( 'Title', 'attachments' ), // label to display
'default' => 'title', // default value upon selection
),
array(
'name' => 'caption', // unique field name
'type' => 'textarea', // registered field type
'label' => __( 'Caption', 'attachments' ), // label to display
'default' => 'caption', // default value upon selection
),
);
$args = array(
// title of the meta box (string)
'label' => 'Bike Images',
// all post types to utilize (string|array)
'post_type' => array( 'post', 'page', 'used' ),
// meta box position (string) (normal, side or advanced)
'position' => 'normal',
// meta box priority (string) (high, default, low, core)
'priority' => 'high',
// allowed file type(s) (array) (image|video|text|audio|application)
'filetype' => null, // no filetype limit
// include a note within the meta box (string)
'note' => 'Attach files here!',
// by default new Attachments will be appended to the list
// but you can have then prepend if you set this to false
'append' => true,
// text for 'Attach' button in meta box (string)
'button_text' => __( 'Attach Files', 'attachments' ),
// text for modal 'Attach' button (string)
'modal_text' => __( 'Attach', 'attachments' ),
// which tab should be the default in the modal (string) (browse|upload)
'router' => 'browse',
// whether Attachments should set 'Uploaded to' (if not already set)
'post_parent' => false,
// fields array
'fields' => $fields,
);
$attachments->register( 'post_attachments', $args );
}
add_action( 'attachments_register', 'post_attachments' );
I'm quite stuck with this so any tips would be much appreciated. I also need to serve the URL's of these image files back via the v2 API. Does this requirement make it even more pertinent to use a plgin such as ACF Pro?
I worry about using too much 3rd party plugins when building bespoke themes not intended to be re-sold etc.
I have a function that I re-use in a number of custom themes when required to fulfil a customers need. I understand this can be achieved now with repeaters in ACF Pro but as I had a working function, I chose to use this.
The problem I'm now facing is I'm really stuck with getting this to show on a custom post type within v5.4.2. I even installed the 'classic Editor' plugin to see if it was an issue with legacy meta fields but no joy...
The code of the function with comments is:
//custom post type attachments for bikes.
add_filter( 'attachments_default_instance', '__return_false' ); // disable the default instance
function post_attachments( $attachments )
{
$fields = array(
array(
'name' => 'title', // unique field name
'type' => 'text', // registered field type
'label' => __( 'Title', 'attachments' ), // label to display
'default' => 'title', // default value upon selection
),
array(
'name' => 'caption', // unique field name
'type' => 'textarea', // registered field type
'label' => __( 'Caption', 'attachments' ), // label to display
'default' => 'caption', // default value upon selection
),
);
$args = array(
// title of the meta box (string)
'label' => 'Bike Images',
// all post types to utilize (string|array)
'post_type' => array( 'post', 'page', 'used' ),
// meta box position (string) (normal, side or advanced)
'position' => 'normal',
// meta box priority (string) (high, default, low, core)
'priority' => 'high',
// allowed file type(s) (array) (image|video|text|audio|application)
'filetype' => null, // no filetype limit
// include a note within the meta box (string)
'note' => 'Attach files here!',
// by default new Attachments will be appended to the list
// but you can have then prepend if you set this to false
'append' => true,
// text for 'Attach' button in meta box (string)
'button_text' => __( 'Attach Files', 'attachments' ),
// text for modal 'Attach' button (string)
'modal_text' => __( 'Attach', 'attachments' ),
// which tab should be the default in the modal (string) (browse|upload)
'router' => 'browse',
// whether Attachments should set 'Uploaded to' (if not already set)
'post_parent' => false,
// fields array
'fields' => $fields,
);
$attachments->register( 'post_attachments', $args );
}
add_action( 'attachments_register', 'post_attachments' );
I'm quite stuck with this so any tips would be much appreciated. I also need to serve the URL's of these image files back via the v2 API. Does this requirement make it even more pertinent to use a plgin such as ACF Pro?
I worry about using too much 3rd party plugins when building bespoke themes not intended to be re-sold etc.
Share Improve this question asked Jul 30, 2020 at 22:00 Ash_FlourishAsh_Flourish 1 1 |1 Answer
Reset to default 0it was completely my bad. I am using this plugin and I updated my functions.php file but the plugin wasn't activated although I thought it had been.
Extension is here & it works in 5.4.2 with Gutenberg blocks if anybody is intereested:
https://github/jchristopher/attachments
本文标签: Custom attachment function not working in v542
版权声明:本文标题:Custom attachment function not working in v5.4.2? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742218706a2435043.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
'attachments_register'
, so this code will never run, unless you're calling your owndo_action
for this somewhere else? – mozboz Commented Jul 31, 2020 at 9:32