(免责声明:我是一名设计师,只是出于需要才成为程序员。这意味着我可以读一些PHP代码,但我自己写起来比较困难。我希望我能做得更好,而这篇文章是为了更好地理解PHP。我可能不知道如何以正确的方式甚至使用正确的术语来问这个问题,所以请耐心等待。)
我有一个自定义的帖子类型插件,它向管理屏幕添加了一些自定义字段,包括一个视频元框字段。Now I need to remove the video field, and I\'m wondering if you can do that fairly easy with some sort of clever unset/unregister/remove function?
我想我已经从插件中识别出了相关的代码,但我当然不想编辑插件本身,而是想在函数文件或类似文件中添加一些代码。你能给我指出正确的方向吗?
<?php
/**
* Class SixTenPressSermonsCustomFields
*/
class SixTenPressSermonsCustomFields extends SixTenPressCustomFields {
/* shortened for sake of the question */
/**
* Define the custom sermon fields.
* @return array
*/
protected function get_file_fields() {
$this->setting = sixtenpresssermons_get_setting();
return array(
array(
\'type\' => \'file\',
\'setting\' => \'mp3\',
\'label\' => __( \'Audio\', \'sixtenpress-sermons\' ),
\'description\' => __( \'Upload the audio file (MP3 format) or paste the URL for external MP3 (eg, SoundCloud).\', \'sixtenpress-sermons\' ),
\'library\' => array( \'audio\' ),
),
array(
\'type\' => \'file\',
\'setting\' => \'video\',
\'label\' => __( \'Video\', \'sixtenpress-sermons\' ),
\'description\' => __( \'Upload the video file (MP4 format) or paste the URL for external video (YouTube, Vimeo).\', \'sixtenpress-sermons\' ),
\'library\' => array( \'video\' ),
),
array(
\'type\' => \'file\',
\'setting\' => \'file\',
\'label\' => __( \'File\', \'sixtenpress-sermons\' ),
\'description\' => __( \'Upload a related file (pdf format). This could be sermon notes or a weekly bulletin.\', \'sixtenpress-sermons\' ),
\'library\' => array( \'application\' ),
),
);
}
我可以在函数中使用一些东西吗。删除上面视频字段的php文件?比如可能:
<?php
function remove_video_field() {
/* not working, just guessing */
unset( sixtenpresssermons_get_setting );
/*specify Video option here somehow? */
}
add_action(\'remove_video_field\',\'SixTenPressSermonsCustomFields\');
// also not working, just me guessing here
最合适的回答,由SO网友:Antti Koskinen 整理而成
您需要查看插件作者是否使用了apply_filters()
在渲染metabox字段的函数中。如果有,那么您可以将过滤功能附加到该挂钩,并从元数据库中删除不必要的字段。
The code below is pseudo-code and for example use only.
类内部可能有一个方法(即函数)沿着这些线。
public function render_metabox_fields() {
$fields = apply_filters( \'filter_available_fields\', $this->get_file_fields() );
// some loop code to actually render the fields...
}
渲染也可能发生在代码库的其他部分-谁知道呢,就像所有第三方插件/主题一样
然后你可以在你的主题functions.php
文件
add_filter(\'filter_available_fields\', \'my_descriptive_function_name\');
function my_descriptive_function_name( $fields ) {
// loop available fields which the apply_filter function provides as a parameter for our function
foreach ($fields as $index => $field) {
// target only certain items with if statement
if ( \'video\' === $field[\'setting\'] ) {
// do something with matched items
unset($fields[$index]);
}
}
// return variable to the apply_filters function so that the original code can continue its operations
return $fields;
}
但是,如果没有可附加自定义代码的过滤挂钩,那么您可能运气不好。您最好查看插件作者或文档。