使用操作、筛选器或挂钩将HTML附加到WordPress插件函数

时间:2019-08-23 作者:Mike Hermary

我不熟悉WordPress操作、过滤器和挂钩,我想知道是否可以使用这三种方法中的一种,使用主题函数将一些HTML附加到定义的插件函数中。php文件。HTML是当前插件主PHP文件中的帮助文本,但我想将其移到函数中。php文件,因为插件将来会更新。带有“追加文本”类的段落应该使用动作、过滤器或挂钩追加到HTML中。

插件功能:

public function insert_upload_form() {
    if ( ! current_user_can( \'upload_files\' ) ) {
        return; //Users must be author or greater
    }

$user_id = $this->get_user_id();
$post_id = $this->get_post_id( $user_id );

?>
<tr valign="top" class="user-metronet-profile-picture">
    <th scope="row"><?php esc_html_e( \'Profile Image\', \'metronet-profile-picture\' ); ?></th>
    <td id="mpp">
        <input type="hidden" name="metronet_profile_id" id="metronet_profile_id" value="<?php echo esc_attr( $user_id ); ?>" />
        <input type="hidden" name="metronet_post_id" id="metronet_post_id" value="<?php echo esc_attr( $post_id ); ?>" />
        <div id="metronet-profile-image">
        <?php
        $has_profile_image = false;
        if ( has_post_thumbnail( $post_id ) ) {
            $has_profile_image = true;
            echo \'<a style="display:block" href="#" class="mpp_add_media">\';
            $thumb_src      = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), \'thumbnail\', false, \'\' );
            $post_thumbnail = sprintf( \'<img style="display:block" src="%s" width="150" height="150" title="%s" />\', esc_url( $thumb_src[0] ), esc_attr__( \'Upload or Change Profile Picture\', \'metronet-profile-picture\' ) );
            echo wp_kses_post( $post_thumbnail );
            echo sprintf( \'<div id="metronet-click-edit">%s</div>\', esc_html__( \'Click to Edit\', \'metronet-profile-picture\' ) );
            echo \'</a>\';
        } else {
            echo \'<a style="display:block" href="#" class="mpp_add_media default-image">\';
            $post_thumbnail = sprintf( \'<img style="display:block" src="%s" width="150" height="150" title="%s" />\', self::get_plugin_url( \'img/mystery.png\' ), esc_attr__( \'Upload or Change Profile Picture\', \'metronet-profile-picture\' ) );
            echo wp_kses_post( $post_thumbnail );
            echo sprintf( \'<div id="metronet-click-edit">%s</div>\', esc_html__( \'Click to Edit\', \'metronet-profile-picture\' ) );
            echo \'</a>\';
        }
        $remove_classes = array( \'dashicons\', \'dashicons-trash\' );
        if ( ! $has_profile_image ) {
            $remove_classes[] = \'mpp-no-profile-image\';
        }
        ?>
            <a id="metronet-remove" class="<?php echo implode( \' \', $remove_classes ); // phpcs:ignore ?>" href="#" title="<?php esc_attr_e( \'Remove profile image\', \'metronet-profile-picture\' ); ?>"><?php esc_html_e( \'Remove profile image\', \'metronet-profile-picture\' ); ?></a>
            <div style="display: none">
                <?php printf( \'<img class="mpp-loading" width="150" height="150" alt="Loading" src="%s" />\', esc_url( self::get_plugin_url( \'/img/loading.gif\' ) ) ); ?>
            </div>
        </div><!-- #metronet-profile-image -->
        <div id="metronet-override-avatar">
            <input type="hidden" name="metronet-user-avatar" value="off" />
            <?php
            //Get the user avatar override option - If not set, see if there\'s a filter override.
            $user_avatar_override = get_user_option( \'metronet_avatar_override\', $user_id );
            $checked = \'\';
            if ( $user_avatar_override ) {
                $checked = checked( \'on\', $user_avatar_override, false );
            } else {
                $checked = checked( true, apply_filters( \'mpp_avatar_override\', false ), false );
            }

            //Filter for hiding the override interface.  If this option is set to true, the mpp_avatar_override filter is ignored and override is enabled by default
            $hide_override = apply_filters( \'mpp_hide_avatar_override\', false );
            if ( $hide_override ) :
                ?>
                <input type="hidden" name="metronet-user-avatar" id="metronet-user-avatar" value="on"  />
                <?php
                else :
                    ?>
                    <br /><input type="checkbox" name="metronet-user-avatar" id="metronet-user-avatar" value="on" <?php echo $checked; // phpcs:ignore ?> /> <label for="metronet-user-avatar"><?php esc_html_e( \'Override Avatar?\', \'metronet-profile-picture\' ); ?></label>
                <?php endif; ?>
        </div><!-- #metronet-override-avatar -->
        <p class="appended-text">
            <strong>Note: optimum image size is 200 pixels wide by 200 pixels tall.<br>
            Maximum file size is 200KB.<br>
            (Your profile image may appear "squished" in this preview, but will appear normal on articles and events you post, and on your author page.)
            </strong>
        </p>
    </td>
</tr>
<?php
/**
 * Allow other plugins to run code after the user profile picture UI.
 *
 * @since 2.3.0
 *
 */
do_action( \'mpp_user_profile_form\', $user_id );
} //end insert_upload_form

1 个回复
SO网友:czerspalace

do\\u操作调用允许您添加add_action 这将在代码中的那个点运行。根据您提供的代码,可以使用mpp_user_profile_form 动作,如:

add_action( \'mpp_user_profile_form\', function( $user_id ) {
?>
<tr>
    <p class="appended-text">
        <strong>Note: optimum image size is 200 pixels wide by 200 pixels tall.<br>
        Maximum file size is 200KB.<br>
        (Your profile image may appear "squished" in this preview, but will appear normal on articles and events you post, and on your author page.)
        </strong>
    </p>
</tr>
<?php
} ); 
否则,您可以附加到#metronet-override-avatar 带javascript的div

相关推荐

POST_ROW_ACTIONS不适用于分层帖子类型

我正在开发一个插件,其中注册了一个自定义帖子类型(CPT),我需要向该特定帖子类型添加自定义行操作。但无法将以下代码连接到post_row_actions 工作:function ttest_copy_button( $actions, $post ) { // var_dump($actions); if( \'ttest\' === $post->post_type ) { // pass nonce to check and ve