插件-将Meta Box输入字段合并为单个可保存的记录

时间:2020-02-15 作者:Nathan Jones

说到编码,我完全是个新手。我已经用一些自定义的帖子类型构建了一个主题。在自定义贴子类型中,我使用生成器工具创建了一个自定义元框。它几乎做了我需要的一切,除了。。。

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

在下面的代码中,您将看到一个名为“文档链接”的字段。在前端,它有一个按钮,允许用户选择媒体库项目,然后将URL插入“document-link”字段/例如mysite。com/library/intro。pdf格式

在我的主题中,链接显示为使用PostTablePro插件的按钮。

由于插件的限制,要显示按钮,插件需要保存的记录保存链接的完整html,如下所示。。。

<a class="btn btn-outline-info btn-block" href="mysite.com/library/intro.pdf" aria-label="Download File"><i class="fa fa-external-link" aria-hidden="true"></i></a>
这很容易,但是。。我想根据文件类型(pdf图标、word图标等)更改按钮的颜色/图标。

在下面的完整代码中,您将看到一个名为“document mime type”的选择字段,它保存文件类型。

我想要实现的是在页面上保存。。。包含普通url的字段“document link”用完整的html格式包装,您将在上面的代码中看到的a类和I类根据在名为“document mime type”的字段中选择的文件类型进行更改。

我想是某种if-then-else的说法,但过去几天我一直在努力解决这个问题,我无法让我的大脑进入一个起始位置。。。。谁能教我如何从几种文件类型开始,我可以从那里开始。。。。THANK YOU

<?php
    /**
     * Everything from here down is the meta box that doesn\'t do what i need it to.
     * im not familiar with below i used a generator online change away it confuses me...
     */
class Rational_Meta_Box {
    private $screens = array(
        \'library_ims\',
    );
    private $fields = array(
        array(
            \'id\' => \'document-link\',
            \'label\' => \'Document Link\',
            \'type\' => \'media\',
        ),
        array(
            \'id\' => \'ims-reference\',
            \'label\' => \'IMS Reference\',
            \'type\' => \'text\',
        ),
        array(
            \'id\' => \'document-mime-type\',
            \'label\' => \'Document MIME Type\',
            \'type\' => \'select\',
            \'options\' => array(
                \'fa fa-file-word-o\' => \'MS Word\',
                \'fa-file-pdf-o\' => \'Adobe PDF\',
                \'fa-file-excel-o\' => \'MS Excel\',
                \'fa-file-powerpoint-o\' => \'MS PowerPoint\',
                \'fa-external-link\' => \'External Link\',
                \'fa-file-video-o\' => \'Video File\',
                \'fa-file-image-o\' => \'Image File\',
                \'fa-file-archive-o\' => \'Archive (ZIP) File\',
                \'fa-file-audio-o\' => \'Audio File\',
                \'fa-globe\' => \'Earth (KMZ) File\',
            ),
        ),
        array(
            \'id\' => \'password\',
            \'label\' => \'Password\',
            \'type\' => \'password\',
        ),
        array(
            \'id\' => \'rev\',
            \'label\' => \'Rev\',
            \'type\' => \'text\',
        ),
        array(
            \'id\' => \'annex-sl-ref\',
            \'label\' => \'Annex SL Ref\',
            \'type\' => \'select\',
            \'options\' => array(
                \'Not Applicable\',
                \'Leadership\',
                \'Planning\',
                \'Support\',
                \'Operation\',
                \'Evaluation\',
                \'Improvement  \',
            ),
        ),
    );

    /**
     * Class construct method. Adds actions to their respective WordPress hooks.
     */
    public function __construct() {
        add_action( \'add_meta_boxes\', array( $this, \'add_meta_boxes\' ) );
        add_action( \'admin_footer\', array( $this, \'admin_footer\' ) );
        add_action( \'save_post\', array( $this, \'save_post\' ) );
    }

    /**
     * Hooks into WordPress\' add_meta_boxes function.
     * Goes through screens (post types) and adds the meta box.
     */
    public function add_meta_boxes() {
        foreach ( $this->screens as $screen ) {
            add_meta_box(
                \'document-overview\',
                __( \'Document Overview\', \'rational-metabox\' ),
                array( $this, \'add_meta_box_callback\' ),
                $screen,
                \'normal\',
                \'high\'
            );
        }
    }

    /**
     * Generates the HTML for the meta box
     * 
     * @param object $post WordPress post object
     */
    public function add_meta_box_callback( $post ) {
        wp_nonce_field( \'document_overview_data\', \'document_overview_nonce\' );
        $this->generate_fields( $post );
    }

    /**
     * Hooks into WordPress\' admin_footer function.
     * Adds scripts for media uploader.
     */
    public function admin_footer() {
        ?><script>
            // https://codestag.com/how-to-use-wordpress-3-5-media-uploader-in-theme-options/
            jQuery(document).ready(function($){
                if ( typeof wp.media !== \'undefined\' ) {
                    var _custom_media = true,
                    _orig_send_attachment = wp.media.editor.send.attachment;
                    $(\'.rational-metabox-media\').click(function(e) {
                        var send_attachment_bkp = wp.media.editor.send.attachment;
                        var button = $(this);
                        var id = button.attr(\'id\').replace(\'_button\', \'\');
                        _custom_media = true;
                            wp.media.editor.send.attachment = function(props, attachment){
                            if ( _custom_media ) {
                                $("#"+id).val(attachment.url);
                            } else {
                                return _orig_send_attachment.apply( this, [props, attachment] );
                            };
                        }
                        wp.media.editor.open(button);
                        return false;
                    });
                    $(\'.add_media\').on(\'click\', function(){
                        _custom_media = false;
                    });
                }
            });
        </script><?php
    }

    /**
     * Generates the field\'s HTML for the meta box.
     */
    public function generate_fields( $post ) {
        $output = \'\';
        foreach ( $this->fields as $field ) {
            $label = \'<label for="\' . $field[\'id\'] . \'">\' . $field[\'label\'] . \'</label>\';
            $db_value = get_post_meta( $post->ID, \'document_overview_\' . $field[\'id\'], true );
            switch ( $field[\'type\'] ) {
                case \'media\':
                    $input = sprintf(
                        \'<input class="regular-text" id="%s" name="%s" type="text" value="%s"> <input class="button rational-metabox-media" id="%s_button" name="%s_button" type="button" value="Upload/Link" />\',
                        $field[\'id\'],
                        $field[\'id\'],
                        $db_value,
                        $field[\'id\'],
                        $field[\'id\']
                    );
                    break;
                case \'select\':
                    $input = sprintf(
                        \'<select id="%s" name="%s">\',
                        $field[\'id\'],
                        $field[\'id\']
                    );
                    foreach ( $field[\'options\'] as $key => $value ) {
                        $field_value = !is_numeric( $key ) ? $key : $value;
                        $input .= sprintf(
                            \'<option %s value="%s">%s</option>\',
                            $db_value === $field_value ? \'selected\' : \'\',
                            $field_value,
                            $value
                        );
                    }
                    $input .= \'</select>\';
                    break;
                default:
                    $input = sprintf(
                        \'<input %s id="%s" name="%s" type="%s" value="%s">\',
                        $field[\'type\'] !== \'color\' ? \'class="regular-text"\' : \'\',
                        $field[\'id\'],
                        $field[\'id\'],
                        $field[\'type\'],
                        $db_value
                    );
            }
            $output .= $this->row_format( $label, $input );
        }
        echo \'<table class="form-table"><tbody>\' . $output . \'</tbody></table>\';
    }

    /**
     * Generates the HTML for table rows.
     */
    public function row_format( $label, $input ) {
        return sprintf(
            \'<tr><th scope="row">%s</th><td>%s</td></tr>\',
            $label,
            $input
        );
    }
    /**
     * Hooks into WordPress\' save_post function
     */
    public function save_post( $post_id ) {
        if ( ! isset( $_POST[\'document_overview_nonce\'] ) )
            return $post_id;

        $nonce = $_POST[\'document_overview_nonce\'];
        if ( !wp_verify_nonce( $nonce, \'document_overview_data\' ) )
            return $post_id;

        if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
            return $post_id;

        foreach ( $this->fields as $field ) {
            if ( isset( $_POST[ $field[\'id\'] ] ) ) {
                switch ( $field[\'type\'] ) {
                    case \'email\':
                        $_POST[ $field[\'id\'] ] = sanitize_email( $_POST[ $field[\'id\'] ] );
                        break;
                    case \'text\':
                        $_POST[ $field[\'id\'] ] = sanitize_text_field( $_POST[ $field[\'id\'] ] );
                        break;
                }
                update_post_meta( $post_id, \'document_overview_\' . $field[\'id\'], $_POST[ $field[\'id\'] ] );
            } else if ( $field[\'type\'] === \'checkbox\' ) {
                update_post_meta( $post_id, \'document_overview_\' . $field[\'id\'], \'0\' );
            }
        }
    }
}
new Rational_Meta_Box;

1 个回复
SO网友:Vitauts Stočka

在你之后用这样的东西foreach 环在此示例中,链接保存在新的元字段中。您可以覆盖document_overview_document-link, 但当在编辑器中打开文档时,您将得到组合的html代码作为链接值。

foreach {
    ...
}

if ( isset( $_POST[ \'document-link\' ] ) ) {
    $html = \'<a class="btn btn-outline-info btn-block" href="\' . esc_url( $_POST[ \'document-link\' ] ) .  \'" aria-label="Download File"><i class="fa \' . sanitize_text_field( $_POST[ \'document-mime-type\' ] ) .  \'" aria-hidden="true"></i></a>\';
} else {
    $html = \'\';        
}

update_post_meta( $post_id, \'document_overview_link_html\', $html );

相关推荐

在WordPress中包含Facebook Javascript SDK

我想在我的wordpress自定义主题中包含Facebook JavaScript SDK。我可以在标准的MVC应用程序或PHP网站上完成,但使用wordpress我不知道如何继续。通常使用enqueue 作用我想直接将其添加到<head> 我的标签header.php 文件,但我不确定这是否有效。我已经阅读了FB文档,他们建议使用插件,但如果我不想使用插件,最好的方法是什么?