Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Wordpress,验证所需的快捷码属性_Php_Wordpress_Shortcode_Wordpress Shortcode - Fatal编程技术网

Php Wordpress,验证所需的快捷码属性

Php Wordpress,验证所需的快捷码属性,php,wordpress,shortcode,wordpress-shortcode,Php,Wordpress,Shortcode,Wordpress Shortcode,我有一个代码,在呈现短代码时验证属性。 但是,当wordpress管理员编辑文章时(保存之前),我需要对shortcode的必需属性进行验证。在wordpress上做到这一点真的很难吗?似乎没有简单的方法可以做到这一点。需要大量的员工: 1。捕获更新后事件: add_action( 'pre_post_update', [ $this, 'validate_post_short_code_attributes' ], 10, 2 ); 2。在帖子中解析您的短代码并进行验证 public fun

我有一个代码,在呈现短代码时验证属性。
但是,当wordpress管理员编辑文章时(保存之前),我需要对shortcode的必需属性进行验证。在wordpress上做到这一点真的很难吗?

似乎没有简单的方法可以做到这一点。需要大量的员工:

1。捕获更新后事件:

add_action( 'pre_post_update', [ $this, 'validate_post_short_code_attributes' ], 10, 2 );
2。在帖子中解析您的短代码并进行验证

public function validate_post_short_code_attributes( $post_id, $post_data ) {

        # If this is just a revision, don't do anything.
        if ( wp_is_post_revision( $post_id ) ) {
            return;
        }
        preg_match_all( '/(\[viatel\s([\S\s]+?)])/', $post_data['post_content'], $matches );

        if ( array_key_exists( 2, $matches ) ) {
            $attrs = shortcode_parse_atts( $matches[2][0] );
            try {
                $this->validate_required_attributes( $attrs );
            } catch ( LogicException $exception ) {
                # Add a notification
                update_option(
                    'viatel_notifications',
                    json_encode( [ 'error', $exception->getMessage() ] )
                );
                # And redirect
                header( 'Location: ' . get_edit_post_link( $post_id, 'redirect' ) );
                exit;
            }
        }
    }
3。使用通知和选项实施黑客攻击


function viatel_notice(){
$notifications=get_选项(“viatel_通知”);
如果(!空($notifications)){
$notifications=json_decode($notifications,true);
#通知[0]=(字符串)通知类型:错误、已更新或更新nag
#通知[1]=(字符串)消息
#通知[2]=(布尔值)是否可驳回?
交换机($notifications[0]){
案例“错误”:#红色
案例“更新”:#绿色
案例“更新nag”:?
$class=$notifications[0];
打破
违约:
#默认为错误,以防万一
$class='error';
打破
}
$is_dismissible='';
if(isset($notifications[2])&&$notifications[2]==true){
$is_dismissible='is_dismissible';
}
回声';
回显“”.$notifications[1]。

”; 回声'; #让我们重新设置通知 更新选项(“viatel通知”,错误); } }
我可能要做的是检测您正在发表文章,并在文章数据上启用一些验证逻辑,不管是使用shortcode还是其他方法,然后启动验证响应。(将其重新添加为评论,而不是回答)感谢您指出了方向。我很感激。
add_action( 'admin_notices', 'viatel_notice' );
function viatel_notice() {
    $notifications = get_option( 'viatel_notifications' );
    if ( ! empty( $notifications ) ) {
        $notifications = json_decode( $notifications, true );
        #notifications[0] = (string) Type of notification: error, updated or update-nag
        #notifications[1] = (string) Message
        #notifications[2] = (boolean) is_dismissible?
        switch ( $notifications[0] ) {
            case 'error': # red
            case 'updated': # green
            case 'update-nag': # ?
                $class = $notifications[0];
                break;
            default:
                # Defaults to error just in case
                $class = 'error';
                break;
        }

        $is_dismissible = '';
        if ( isset( $notifications[2] ) && $notifications[2] == true ) {
            $is_dismissible = 'is_dismissible';
        }
        echo '<div class="' . $class . ' notice ' . $is_dismissible . '">';
        echo '<p>' . $notifications[1] . '</p>';
        echo '</div>';
        # Let's reset the notification
        update_option( 'viatel_notifications', false );
    }
}