猿问

如何在WordPress中注销之前执行一些代码

我正在尝试为WordPress网站构建自定义退出页面。


我禁用了wp-login.php以下.htaccess代码。


<Files wp-login.php>

deny from all

</Files>

如果您访问此页面,您将收到403禁止响应。


现在,我要在用户注销之前执行一个“之前”功能。


function execute_this() {

  wp_logout();

}

事情是来自互联网的所有建议都在使用以下代码。


add_action('wp_logout', 'execute_this');

但是此挂钩执行“ AFTER”成功注销,在我看来,由于wp-login.php无法访问,因此无法完成。


这就是为什么我要注销用户“之前”将其转到wp-login.php文件的原因。


是否有引发这种情况的挂钩/动作?


我也尝试做这样的事情(使用我创建的页面自定义注销页面):


add_filter( 'logout_url', 'wpse_58453_logout_url' );

function wpse_58453_logout_url( $default )

{

    return plugin_dir_url(__FILE__) . 'logout.php';

}

然后在我的 logout.php


require_once('../../../wp-includes/pluggable.php');

wp_logout();


我知道这是错误的方法,因为直接执行PHP文件从来都不是一个好主意,而且在执行此类操作时我无法访问WordPress挂钩。


有什么办法可以解决这个问题?


阿晨1998
浏览 197回答 1
1回答

UYOU

我对wordpress的经验不是很丰富,但是我帮助维护的站点之一具有自定义注销url(/tp-logout),并且该主题具有以下代码来执行注销function tp_custom_logout() {&nbsp; &nbsp; &nbsp; &nbsp; if ( is_page( 'tp-logout' ) && is_user_logged_in() ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wp_destroy_current_session();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wp_clear_auth_cookie();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $url_logout = home_url();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wp_redirect( $url_logout );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit();&nbsp; &nbsp; &nbsp; &nbsp; }}add_action( 'template_redirect', 'tp_custom_logout' );它看起来与默认的WP注销功能极为相似,因此不知道为什么要添加它,但是也许您可以按照此方法添加自定义代码。我将调用该函数wp_logout()进行实际注销,而不是复制代码(并且不调用wp_logout操作钩子),以使其更加友好。
随时随地看视频慕课网APP
我要回答