如何在WordPress网站中添加和访问自定义的PHP代码?

huangapple go评论56阅读模式
英文:

How to add and access custom php code in WordPress site?

问题

我需要将一些自己的php文件添加到现有的WordPress站点(来自Bitnami),以便可以从第三方服务访问。我不需要在我的代码中访问WordPress的任何内容,但我需要使我的代码可以通过URL从第三方服务访问。

像这样:

https://myWordPressSite.com. <- 普通的WP站点

https://myWordPressSite.com/myCustomDirectory/generateSerial.php <- 我的代码
https://myWordPressSite.com/myCustomDirectory/doSomething1.php <- 我的代码
https://myWordPressSite.com/myCustomDirectory/doSomething2.php <- 我的代码

我该如何将我的代码目录添加到WordPress文件结构中,并如何通过URL访问它?

英文:

I need to add some of my own php files to an existing WordPress site (from Bitnami) so it can be accessed from a 3rd-party service. I don't need to access anything from WordPress in my code, but I need my code to be accessible from a url for 3rd-party services.

Like this:

https://myWordPressSite.com. <- normal WP site

https://myWordPressSite.com/myCustomDirectory/generateSerial.php <- my code
https://myWordPressSite.com/myCustomDirectory/doSomething1.php <- my code
https://myWordPressSite.com/myCustomDirectory/doSomething2.php <- my code

How can I add my directory of code to the WordPress file structure, and how to access it from a URL?

答案1

得分: 0

你可以将$_GET或$_POST添加到WordPress插件的头部作为插件。

你需要一个插件的头部

/*
Plugin Name: name
Plugin URI: http://www.com/
Description: test
Version: 1.0
Author: your name
Author URI: http://www.com/
*/

代码的第一部分将是。你在这里放入的代码将在每个页面调用时显示在头部

function test_head() {
	echo $_GET[&#39;name it some thing&#39;]; 
	echo $_POST[&#39;name it some thing&#39;]; 			
}

要调用该函数,您需要添加以下代码行

add_action( &#39;wp_head&#39;, &#39;test_head&#39; );

要了解更多关于add_action的信息,您可以阅读它在以下链接:https://developer.wordpress.org/reference/functions/add_action/

在激活插件之后,
您现在可以根据您添加到头部的内容使用GET或POST来访问您的网站。

通过将其作为插件来实现,您可以使用WordPress的基本功能。

祝你好运
Karl K

英文:

You can add a $_GET OR $_POST to the header as a plugin for WordPress.

You will need a header of the plugin

/*
Plugin Name: name
Plugin URI: http://www.com/
Description: test
Version: 1.0
Author: your name
Author URI: http://www.com/
*/

The first part of the code would be. The code you put in here would show in the header when each page is call

function test_head() {
	echo $_GET[&#39;name it some thing&#39;]; 
	echo $_POST[&#39;name it some thing&#39;]; 			
}

To call the function you need to add this line of code

add_action( &#39;wp_head&#39;, &#39;test_head&#39; );

To learn more about add_action you can read it at
https://developer.wordpress.org/reference/functions/add_action/

After you a Activate the plugin
You can now call your web site with GET or POST base on what you added to the header.

By doing it was a plugin you can use the WordPress backbone function

Good Luck
Karl K

答案2

得分: 0

在您的主题根目录中创建一个名为myCustomDirectory的文件夹,并将您的PHP文件放在其中。

然后在您的functions.php中添加以下内容:

这将处理重写规则:

add_action('init', 'custom_add_rewrite_rule');
function custom_add_rewrite_rule() {
    add_rewrite_rule('^myCustomDirectory/generateSerial.php', 'index.php?generateSerial=true', 'top');
}

这将处理WordPress查询变量

function add_query_vars_filter( $vars ){
    $vars[] = 'generateSerial';
    return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );

这将告诉WordPress拉取正确的文件:

function load_custom_template($template){
    if(get_query_var('generateSerial') ){
         $template = get_template_directory() . "/myCustomDirectory/generateSerial.php";
    }

    return $template;  
}

add_filter('template_include', 'load_custom_template');

确保在完成后刷新您的重写规则,方法是转到:设置 > 永久链接,然后单击“保存更改”

英文:

Create a folder in your theme's root folder called myCustomDirectory and place your php file inside that.

then add to your functions.php the following:

This will handle the Rewrite Rules:

add_action(&#39;init&#39;, &#39;custom_add_rewrite_rule&#39;);
function custom_add_rewrite_rule() {
    add_rewrite_rule(&#39;^myCustomDirectory/generateSerial.php&#39;, &#39;index.php?generateSerial=true&#39;, &#39;top&#39;);
}

This will handle the WordPress Query Vars:

function add_query_vars_filter( $vars ){
    $vars[] = &#39;generateSerial&#39;;
    return $vars;
}
add_filter( &#39;query_vars&#39;, &#39;add_query_vars_filter&#39; );

This will tell wordpress to pull the correct file:

function load_custom_template($template){
    if(get_query_var(&#39;generateSerial&#39;) ){
         $template = get_template_directory() .&quot;/myCustomDirectory/generateSerial.php&quot;;
    }

    return $template;  
}

add_filter(&#39;template_include&#39;, &#39;load_custom_template&#39;);

make sure to flush your rewrite rules when you are done by going to: Settings > Permalinks and clicking 'save changes'

答案3

得分: 0

代码部分不翻译,以下是翻译好的内容:

"It turns out it was much simpler than I thought, or any of the other answers presented here."

原来比我想的要简单得多,或者比这里提出的任何其他答案都简单。

"The answer is to just add a directory of my own code inside the WordPress home folder."

答案就是在WordPress主文件夹内添加一个我自己的代码目录。

"In the case of a AWS/Lightsail/Bitnami/Wordpress package, the home directory is:"

对于AWS/Lightsail/Bitnami/WordPress包,主文件夹路径是:

"/opt/bitnami/wordpress"

"/opt/bitnami/wordpress"

"So I created a directory and put my code in it.."

所以我创建了一个目录,并将我的代码放在其中。

"/opt/bitnami/wordpress/myDirectory"
"/opt/bitnami/wordpress/myDirectory/generateSerial.php"
"/opt/bitnami/wordpress/myDirectory/doSomething.php"

"And it was then accessible from a url like:"

然后,可以通过以下URL访问:

"https://myWordPressSite"
"https://myWordPressSite/generateSerial.php" //等等

"One small catch is that some WordPress packages and some other Bitnami packages are laid out a bit different, so check on your specific installation."

有一个小问题是,某些WordPress包和其他一些Bitnami包的布局略有不同,因此请检查您的特定安装情况。

英文:

It turns out it was much simpler than I thought, or any of the other answers presented here.

The answer is to just add a directory of my own code inside the WordPress home folder.

In the case of a AWS/Lightsail/Bitnami/Wordpress package, the home directory is:

/opt/bitnami/wordpress

So I created a directory and put my code in it..

/opt/bitnami/wordpress/myDirectory
/opt/bitnami/wordpress/myDirectory/generateSerial.php
/opt/bitnami/wordpress/myDirectory/doSomething.php

And it was then accessible from a url like:

https://myWordPressSite
https://myWordPressSite/generateSerial.php //etc

One small catch is that some WordPress packages and some other Bitnami packages are laid out a bit different, so check on your specific installation.

huangapple
  • 本文由 发表于 2023年2月7日 04:19:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75366168.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定