【问题标题】:Wordpress hook before edit.php loads在 edit.php 加载之前的 Wordpress 钩子
【发布时间】:2018-01-05 06:56:56
【问题描述】:

这似乎很琐碎,但我找不到解决方案。

我有帖子的自定义过滤器。并不是每个用户都可以编辑每个帖子。它取决于自定义元标记。 (这不是那么重要)

重要的是,如果他们使用类似 `

的 URL

/edit.php?post=467

他们可以访问我“不允许他们”发布的帖子。

我正在寻找的是,是否有可能像

那样在钩子中获取 URL

admin_init

所以我可以做类似的事情

if (!isset($_GET['post'])){ //redirect }

if ($_GET['post'] == $forbidenID) { //redirect }

谢谢各位。 :-)

【问题讨论】:

  • 您可能会在网站上获得更多关于 WordPress 开发问题的帮助:wordpress.stackexchange.com
  • 如果没有人帮助我,我可能会尝试。

标签: php wordpress


【解决方案1】:

编辑:我刚刚意识到你在 edit.php 中,意思是 WordPress 的管理区域......在那种情况下,正如你提到的 `admin_init' 操作https://codex.wordpress.org/Plugin_API/Action_Reference/admin_init

尝试类似:

** 快速的事情:你说如果isset($_GET['post']) 正确,你想重定向.. 在你的例子中,你说!isset($_GET['post']) 所以必须验证你的意思。

function restrict_admin_with_redirect() {

    if (isset($_GET['post'])) {
        wp_redirect( site_url() ); 
        exit;
    }
}

add_action( 'admin_init', 'restrict_admin_with_redirect', 1 );

/////////////////////////////////////// ////////////////////////// ///////////////////////////////////////// /////////////////////////p>

其他信息:

如果正在执行的页面不在管理区,而是在WordPress的前端,就像普通页面或帖子一样:

有几个钩子可以用来做这个。您也可以选择编辑主题的 header.php 文件。

查看the_content 过滤器:https://codex.wordpress.org/Plugin_API/Filter_Reference/the_content

您可以将其添加到您的 functions.php 文件中(最好使用子主题,这样在更新主主题时更改不会被覆盖。)

function my_the_content_filter($content) {

  if (isset($_GET['post'])){
     return 'You do not have access to this page.';
  }

  // otherwise returns the database content
  return $content;
}

add_filter( 'the_content', 'my_the_content_filter' );

或者在 header.php 文件的最开始处(在输出任何 html 之前),添加如下内容:

  if (isset($_GET['post'])){
     wp_redirect( {url to redirect to} );
     exit();
  }

【讨论】:

  • 我在发布之前已经尝试过这个,但由于某种原因无法访问 $_GET 全局变量。我可能会再试一次并在那里发布我想出的东西
猜你喜欢
  • 2016-09-01
  • 2014-12-01
  • 1970-01-01
  • 2013-01-25
  • 2013-01-03
  • 2013-11-17
  • 2015-04-02
  • 2022-01-21
  • 1970-01-01
相关资源
最近更新 更多