admin管理员组

文章数量:1389510

(Disclaimer: I am a designer by choice and a programmer only by necessity. Meaning I can somewhat read PHP code, but I have a harder time writing it myself. I wish I could be better though, and this post is an attempt at understanding PHP a bit better. I probably don't know enough to ask this question in theright way or even using the right terms, so please bear with me. )

I have a custom post type plugin that is adding some custom fields to the admin screens, including a video meta box field. 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?

I think I have identified the relevant piece of code from the plugin, but of course I don't want to edit the plugin itself - but rather add some code in a functions file or similar. Can you give me some pointers in the right direction?

<?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' ),
            ),
        );
    }

Can I use something in the functions.php file to remove the video field above? Like maybe:

<?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

(Disclaimer: I am a designer by choice and a programmer only by necessity. Meaning I can somewhat read PHP code, but I have a harder time writing it myself. I wish I could be better though, and this post is an attempt at understanding PHP a bit better. I probably don't know enough to ask this question in theright way or even using the right terms, so please bear with me. )

I have a custom post type plugin that is adding some custom fields to the admin screens, including a video meta box field. 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?

I think I have identified the relevant piece of code from the plugin, but of course I don't want to edit the plugin itself - but rather add some code in a functions file or similar. Can you give me some pointers in the right direction?

<?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' ),
            ),
        );
    }

Can I use something in the functions.php file to remove the video field above? Like maybe:

<?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

Share Improve this question asked Apr 1, 2020 at 9:05 Anders CarlénAnders Carlén 156 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You'll need to see, if the plugin author has used apply_filters() in the function that renders the metabox fields. If there is one, then you can attach your filtering function to that hook and remove unnecessary fields from the metabox.

The code below is pseudo-code and for example use only.

Inside the class there could be a method (i.e. function) along these lines.

public function render_metabox_fields() {
  $fields = apply_filters( 'filter_available_fields', $this->get_file_fields() );
  // some loop code to actually render the fields...  
}

The rendering might also happen in some other part of the codebase - who knows, as with all 3rd party plugins/themes.

You could then do this in your theme's functions.php file,

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;
}

But, if there are no filtering hooks to attach custom code to, then you're probably out of luck. You better check from the plugin author or documentation.

本文标签: pluginsPHP basics help in WP contextremove a classfunction