最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - Displaying admin notice dynamically

programmeradmin1浏览0评论

I would like show the admin notice while post text is being edited (before Save post). I suppose admin_notice hook wont work in this (it doesn't for me). Any idea how to show some error message to alert the user that Post content is not going to be accepted ?

I prefer inline display like admin notice instead of popups that could be blocked.

Following doesn't work

function secure_oembed_filter($html, $url, $attr, $post_ID) {

    if (strlen($html) > 0 && strpos($html, "http://")) {
        //wp_die("Unsecure link in embeds", "Unsecured post");
    } else {
        add_action('admin_notices', array($this, 'show_error'));
    }
    return $html;
}

private function show_error () {

    echo '<div class="error"><p>This is an error</p></div>';
}

I would like show the admin notice while post text is being edited (before Save post). I suppose admin_notice hook wont work in this (it doesn't for me). Any idea how to show some error message to alert the user that Post content is not going to be accepted ?

I prefer inline display like admin notice instead of popups that could be blocked.

Following doesn't work

function secure_oembed_filter($html, $url, $attr, $post_ID) {

    if (strlen($html) > 0 && strpos($html, "http://")) {
        //wp_die("Unsecure link in embeds", "Unsecured post");
    } else {
        add_action('admin_notices', array($this, 'show_error'));
    }
    return $html;
}

private function show_error () {

    echo '<div class="error"><p>This is an error</p></div>';
}
Share Improve this question asked Jul 11, 2016 at 18:03 rysvrysv 1212 bronze badges 2
  • 1 This would be tricky server side, since you want to keep existing content unchanged, but at the same time keep user's edits (losing them would be horrible UX). Had you considered doing it with JS client-side? – Rarst Commented Jul 11, 2016 at 18:06
  • yes merely flag to the user that the content wont be acceptable, will explore JS – rysv Commented Jul 11, 2016 at 18:43
Add a comment  | 

1 Answer 1

Reset to default 0

In that case wp_remote_get() will work fine.

Create a notice.php file and uploaded it on my server.

Then I have added these code into my plugin and it works for me.

<?php
$remote_data = wp_remote_get( 'http://example/notice.php' );
$remote_body = wp_remote_retrieve_datat( $remote_data );
?>

<div class="notice">
    <?php echo $remote_body ?>
</div>

Hope that helps.

发布评论

评论列表(0)

  1. 暂无评论