使用创建自定义操作add_filter(post_action_{$action});
即使只是为了测试,编辑核心文件也是不好的做法。我更喜欢将源代码下载到我自己的机器上,并找到钩子的触发位置和方式<无论如何,这部分源代码中的文档很差,但我已经研究了一段时间了,看起来
post.php 文件<第一个是
replace_editor 在
switch 接近结尾的语句
post.php 在
case 属于
action=edit. 这使得经典的WordPress编辑器可以被其他东西代替,比如现在附带的Gutenberg编辑器<我使用的第二个钩子是
post_action_{$action}. 这是在
default: 的情况
switch 声明,意思是如果
action=anything_other_than_given_cases 在URL中,这将触发
在
switch 然而,声明,
wp_redirect( admin_url( \'edit.php\' ) ); 调用,在执行连接到
post_action_{$action} 将您重定向到
edit.php.
这与Elementor的做法类似,Elementor有自己的习惯
action 在的URL中使用
post.php, 与查询字符串、GET和AJAX有很大关系,因此如果您对它们不是很熟悉,那么这就是一个开始。很抱歉我说得太多了,但这里有一个
post_action_{$action}.
<?php
add_action( \'post_action_your_action_name\', \'connected_function\' );
function connected_function($post_id) {
//Add your functionality here.
//This will execute when /post.php?post=$post_id&action=your_action_name
}
//After execution we return to default: case in post.php
//Where the wp_redirect() will be called.
如果需要添加链接以访问此页面,
post_row_actions 钩子在中的立柱下添加链接
edit.php 文件,如;编辑(""E;“垃圾”"E;“查看”;,等等,如下所示:
<?php
add_filter( \'post_row_actions\', \'add_links\' );
function add_links($actions, $post) {
$url = add_query_arg(
[
\'post\' => $post->ID,
\'action\' => \'your_action_name\',
],
admin_url( \'post.php\' )
);
$actions[\'your_action_name\'] = sprintf(
\'<a href="%1$s">%2$s</a>\',
$url,
\'Actual Link Text\'
);
return $actions;
}
这会在您的帖子下方创建一个链接,您可以单击该链接将您重定向到要添加到的自定义操作
post.php, i、 e。
/post.php?post=$post_id&action=your_action_name. 此外,别忘了检查当前帖子是否属于要添加此功能的类型。希望这有帮助。