我想使用URL在前端显示内容。例如
域。tld/?我的插件(&P);确认=键(&C);mail=mail,但我不知道怎么走去做吧。例如,我有一个用户单击电子邮件中的确认链接,我想向他显示一个包含一些文本的页面。
WordPres中必须存在真实页面我不希望插件用户手动添加页面,甚至向其主题功能添加代码。php
我想使用URL在前端显示内容。例如
域。tld/?我的插件(&P);确认=键(&C);mail=mail,但我不知道怎么走去做吧。例如,我有一个用户单击电子邮件中的确认链接,我想向他显示一个包含一些文本的页面。
WordPres中必须存在真实页面我不希望插件用户手动添加页面,甚至向其主题功能添加代码。php
我已经找到了我一直在寻找的解决方案。如果其他人需要这个,我想把它留在这里。使用此解决方案,插件的用户不需要做任何事情(创建页面或复制文件)。
因此,首先我们将动作添加到WP中
add_action( \'init\', \'check_url\' );
在这里,我们正在寻找我们的URLfunction check_confirm_url() {
return false !== strpos( $_SERVER[ \'REQUEST_URI\' ], \'/myplugin/confirm\' );
}
如果URL匹配,我们将在此处添加内容function check_url() {
if( check_confirm_url() ) {
add_filter( \'the_posts\', \'confirm_page\' );
}
}
这是我们可以做所有事情的页面function confirm_page( $posts ) {
//do all the stuff here
$posts = null;
$post = new stdClass();
$post->post_content = "Confirm Content";
$post->post_title = "Confirm";
$post->post_type = "page";
$post->comment_status = "closed";
$posts[] = $post;
return $posts;
}
如果我们现在输入这个URL,我们就有了自己的页面,可以使用GET数据。http://domain.tld/myplugin/confirm?mail=MAILADRESS&key=KEY
WordPres中必须存在真实页面?
是的,您需要创建一个页面,但它不必包含任何内容。
假设您创建了一个名为“确认消息”的新页面。URL将是http://example.com/confirmation-message
.
现在将以下内容添加到functions.php
您的主题:
/**
* Adds a confirmation message to the content of a page.
*
* @param string $content The current content of the page.
* @return string The new content of the page.
*/
function show_confirmation_message($content) {
// Don\'t add message if \'myplugin\', \'confirmkey\' and \'mail\' are not in the URL.
if (!isset($_GET[\'myplugin\']) || empty($_GET[\'confirmkey\']) || empty($_GET[\'mail\'])) {
return $content;
}
// Don\'t add message if we\'re not on a page and not in the main query.
if( !is_page() || !is_main_query() ) {
return $content;
}
// Add the logic to show a confirmaiton message here...
$content.= \'This is the confirmation message.\';
return $content;
}
add_filter(\'the_content\', \'show_confirmation_message\');
现在,如果您转到:http://example.com/confirmation-message?myplugin&confirmkey=KEY&mail=MAIL
您应该会看到确认消息。
我有一个需要帮助的问题,因为我自己找不到解决办法。我接管了一个网站,之前有人在那里创建了一个自制插件。。使用默认插件“Contact Form 7”,用户可以在页面上创建帖子。()https://gyazo.com/c8b20adecacd90fb9bfe72ad2138a980 )关于自行创建的插件“Contact Form 7 extender”,帖子是通过PHP代码在后台生成的(https://gyazo.com/115a6c7c9afafd2970b66fd421ca76a3)其工作原理如下:如果