【问题标题】:Wordpress custom PHP file returning 404 when accessedWordpress 自定义 PHP 文件在访问时返回 404
【发布时间】:2019-11-03 08:40:18
【问题描述】:

我正在运行自定义脚本(带有一些功能的 ajax)。 问题是并非所有 .php 文件都已加载...

当我访问 URL/whatever.php 时,它显示 404 not found.

我尝试更改 htaccess 文件,尝试将该文件移动到不同的目录。但是我可以通过这种方式访问​​图像 - 所以重写可能有问题?

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress```

404 page displaying...

【问题讨论】:

  • whatever.php 文件在哪里?在 WP 根目录中?
  • 我试图把它放在任何地方 - 根目录、插件、主题......总是 404。我试图更新永久链接等等......我禁用了所有插件和主题,它们都是一样的
  • 如果您想将它称为 URL/whatever.php,它应该在 root 中,并且它应该以这种方式工作。其他 Wordpress 页面是否正常工作?
  • 页面/帖子正在工作,它只是 .php 文件不工作
  • 一种解决方法,可能是创建一个受密码保护的页面,并为该页面创建一个模板,您可以在其中运行您的功能。

标签: php wordpress apache http-status-code-404


【解决方案1】:

functions.php

add_action( 'wp_ajax_your_custom_function', 'custom_function_init' );
add_action( 'wp_ajax_nopriv_your_custom_function', 'custom_function_init' );

function custom_function_init() {
    $fields = $_REQUEST;
    //do whatever you want
    echo 'return something';
    die();
}

到'footer.php'

<script>
    var ajax_url = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
</script>

还有你的 ajax 东西,在 JS 文件中

function do_ajax(data) {
    if( xhr != null ) {
        xhr.abort();
        xhr = null;
    }

    xhr = $.ajax({
        url: ajax_url,
        timeout: 3600,
        type: "POST",
        cache: false,
        data: ({
            action:'your_custom_function',
            data:data
        }),

        beforeSend: function() {
        },
        success: function( data ) {
            console.log(data)   

        },
        error: function( jqXHR, textStatus, errorThrown ) {
            console.log( 'The following error occured: ' + textStatus, errorThrown );
        },
        complete: function( jqXHR, textStatus ) {
        }
    });
}

【讨论】:

  • 感谢您的输入...我会回复我的网站回复的内容'404 not found admin-ajax.php'
  • 能否提供你在 fuctions.php、footer 和 JS 中的内容?
  • 一切都是正确的 - 它在另一个站点(同一服务器)上进行了测试并且它可以正常工作。由于未知原因,它无法在此网站上运行
猜你喜欢
  • 2013-11-23
  • 2019-04-09
  • 1970-01-01
  • 1970-01-01
  • 2021-10-27
  • 1970-01-01
  • 2017-10-16
  • 2022-10-02
  • 1970-01-01
相关资源
最近更新 更多