如何在wordpress functions.php文件中运行脚本 [英] How to run script in wordpress functions.php file

查看:68
本文介绍了如何在wordpress functions.php文件中运行脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



上下文是我有一个cron作业-我在php中有一个脚本.什么
它确实是确定目录中最旧的几个文件夹并删除
他们.然后,它还会对wordpress数据库进行更新.

从浏览器URL运行时,它可以单独运行,而不会出现任何问题.
但是我正在使用Crontrol wordpress插件,并试图将我的工作集成到其中.
该插件的建议是将我的工作放到functions.php文件中.
我的主题文件夹.然后使用动作挂钩.

我能够成功设置一个简单的工作,该工作将通过
发送电子邮件 wp_mail().但是,当我尝试将脚本集成进去时,它不起作用.
我的脚本中包含函数,因此当我在functions.php文件中使用include(''myfile.php'')时,它会中断吗?

我也尝试将代码直接放置在functions.php文件中,但我开始
我的全局变量有问题.

任何人都有在functions.php文件中创建更长代码的经验吗?
我的脚本包含一个递归函数,用于确定
中最早的x文件 目录,然后将其删除.然后使用
记录更新数据库 删除了什么.

最后,我无法在主机设置面板上设置cron作业,这就是为什么
我不得不尝试这种Crontrol插件方法.

非常感谢,
Brian

Hi,

the context is I have a cron job - a script that I have in php. What
it does is determine the oldest few folders in a directory and deletes
them. It then also performs an update to the wordpress database.

Alone when running from browser URL, it works without any problems.
However I am using Crontrol wordpress plugin and trying to integrate my job into it.
What the plugin has suggested, is to put my job into the functions.php file
of my theme folder. And then use an action hook.

I was able to successfully set up a simple job that sends an email with
wp_mail(). But when I tried integrating my script in, it does not work.
My script has functions in it, so does it break when I use include(''myfile.php'') in my functions.php file?

I also tried placing the code directly in the functions.php file, but I start
having problems with my global variables.

Anyone has experience creating lengthier code in the functions.php file?
My script contains a recursive function to determine the oldest x files in a
directory, and then deletes them. It then updates the database with records of
what was deleted.

Lastly, I could not set up a cron job at the hosting settings panel, thats why
I had to try this Crontrol plugin method.

Many thanks,
Brian

推荐答案

大家好,

在查看functions.php文件时,我已经找到了自己的答案
来自其他模板.

对我有用的是我在外部脚本中移动了所有全局变量
进入主要的functions.php文件.我还从
转移了所有功能 脚本转换为functions.php.

我概述了以下结构,以使那些要求相同的人受益.
问题:

////我在functions.php中的代码的结构

include_once(``您需要什么文件'');
全局变量x,y;
x = blah;
y = blahh;

add_action(''hook name'',``myFunction_name'');

函数myFunction_name(){
include_once(TEMPLATEPATH .``/externalScript.php'');
}

函数function_from_external_script_shifted_here(){
//做点事
}

////结束
Hi everyone,

I have sort of found my own answer while looking at functions.php file
from other templates.

What worked for me was I shifted any global variables in my external script
out into the main functions.php file. I also shifted any functions from my
script into functions.php.

I outline the structure below for the benefit of those asking the same
question:

////structure for my code in functions.php

include_once(''what file you need'');
globals variables x, y;
x= blah;
y= blahh;

add_action(''hook name'', ''myFunction_name'');

function myFunction_name() {
include_once (TEMPLATEPATH . ''/externalScript.php'');
}

function function_from_external_script_shifted_here() {
//do something
}

////end


这篇关于如何在wordpress functions.php文件中运行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆