Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
编辑wordpress样式表并使用php&;AJAX_Php_Jquery_Ajax_Wordpress - Fatal编程技术网

编辑wordpress样式表并使用php&;AJAX

编辑wordpress样式表并使用php&;AJAX,php,jquery,ajax,wordpress,Php,Jquery,Ajax,Wordpress,我想在我的网站上创建一个设置部分,这样我就可以更改样式,所以如果我选择蓝色,它会将背景更改为蓝色,并保存外部样式表,这样当我重新加载页面时,它仍然是蓝色的 我不知道如何做到这一点,所以它只是目前的基本代码 我知道我需要html复选框代码,然后jquery将记录所选内容,ajax和php将更新样式表 我只是不知道怎么做 下面是html代码 <div class="container"> <input type="radio" name="colours" value="red

我想在我的网站上创建一个设置部分,这样我就可以更改样式,所以如果我选择蓝色,它会将背景更改为蓝色,并保存外部样式表,这样当我重新加载页面时,它仍然是蓝色的

我不知道如何做到这一点,所以它只是目前的基本代码

我知道我需要html复选框代码,然后jquery将记录所选内容,ajax和php将更新样式表

我只是不知道怎么做

下面是html代码

<div class="container">
  <input type="radio" name="colours" value="red" checked> red
  <input type="radio" name="colours" value="blue">blue
</div>
以下是我目前掌握的php:
// edit these values to match your database information
$server = "localhost";
$user = "******";
$password = "*******";
$db = "******";

$con = mysql_connect($server,$user,$password); 

if (!$con) {
die("database connection error");
} else {

mysql_select_db($db, $con);

$file = 'get_site_url();/style.css';
// Open the file to get existing content
$current = file_get_contents($file);

// Write the contents back to the file
$test = file_put_contents($current, $current);

echo '

<form action="" method="POST">
<textarea name="stylesheet">'. $current .'</textarea>
<input type="submit">
</form>
';

}
mysql_close($con);
?>
//编辑这些值以匹配数据库信息
$server=“localhost”;
$user=“*******”;
$password=“*******”;
$db=“*******”;
$con=mysql\u connect($server、$user、$password);
如果(!$con){
die(“数据库连接错误”);
}否则{
mysql\u select\u db($db,$con);
$file='get_site_url();/style.css';
//打开文件以获取现有内容
$current=file\u get\u contents($file);
//将内容写回文件
$test=文件内容($current,$current);
回声'
“.$current。”
';
}
mysql_close($con);
?>

除了一般警告限制整个世界使用此面板之外,您还需要先检查提交的表单。您可能需要检查$\u POST['submit'],并为submit按钮指定一个名称:

<form action="" method="POST">
<textarea name="stylesheet"><?php echo $current; ?></textarea>
<input type="submit" name="submit" value="Submit">
</form>
不确定您的sql需要什么,但您没有使用该连接。 整个事情将是:

<?php
if(isset($_POST['submit']))
{
    $stylesheet = $_POST['stylesheet'];
    $stylesheet = preg_replace('/(background:)(.*)(;)/', '$1'.$_POST['colours'].'$3', $_POST['colours'], $stylesheet);
    $fp = fopen( get_site_url() .'/style.css', 'w');
    fwrite($fp, $stylesheet);
    fclose($fp);
}
else
{
    $filename = get_site_url() .'/style.css';
    $handle = fopen($filename, "r");
    $stylesheet = fread($handle, filesize($filename));
    fclose($handle);
}
?>
<form action="" method="POST">
<textarea name="stylesheet"><?php echo $stylesheet; ?></textarea>
<input type="radio" name="colours" value="red" checked> red
<input type="radio" name="colours" value="blue">blue
<input type="submit" name="submit" value="Submit">
</form>

红色
蓝色
假设
get\u site\u url()
解析为该文件的服务器路径。您可能需要使用
$\u服务器['DOCUMENT\u ROOT']/style.css'
取决于此文件实际驻留的位置

另一方面,本示例还假设您从样式表的内容开始。可以使用正则表达式查找并替换要编辑的样式的值。您将希望在实际案例中更加具体,因为这将替换背景:和之间的任何内容

以下是在wp插件中使用ajax的链接:


使用
$\u SERVER['DOCUMENT\u ROOT']设置一个插件,作为ajax需求的请求处理程序,非常简单/css'
是一个
no
。。它将在多站点上中断。仅使用内部WP函数。。。
if(isset($_POST['submit']))
{
    $fp = fopen( get_site_url() .'/style.css', 'w');
    fwrite($fp, $_POST['stylesheet']);
    fclose($fp);
}
<?php
if(isset($_POST['submit']))
{
    $stylesheet = $_POST['stylesheet'];
    $stylesheet = preg_replace('/(background:)(.*)(;)/', '$1'.$_POST['colours'].'$3', $_POST['colours'], $stylesheet);
    $fp = fopen( get_site_url() .'/style.css', 'w');
    fwrite($fp, $stylesheet);
    fclose($fp);
}
else
{
    $filename = get_site_url() .'/style.css';
    $handle = fopen($filename, "r");
    $stylesheet = fread($handle, filesize($filename));
    fclose($handle);
}
?>
<form action="" method="POST">
<textarea name="stylesheet"><?php echo $stylesheet; ?></textarea>
<input type="radio" name="colours" value="red" checked> red
<input type="radio" name="colours" value="blue">blue
<input type="submit" name="submit" value="Submit">
</form>