Подскажите, как добавить php скрипт в статью вордпресс
Добавил в public_html папку img, содержащую скрипт выбора шрифтов. При заходе на www.mysite.ru/img отображается:
Содержимое папки img:
Нужно сделать так, чтоб этот скрипт выводился внутри статьи. Я совсем не шарю, подскажите, пожалуйста.
index.php:
<?
$fonts = file('fonts.txt');
$name = $_GET['name'];
if($name=='')
$name = 'Введите текст';
echo '<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Выбор шрифта</title>
<style>
body{
font:10px Verdana, sans-serif;
color:#464544;
}
a{
font:10px Verdana;
color:#464544;
margin:0 5px;
}
.imgfonts img{
font:10px Verdana;
color:#464544;
}
.imgfonts p{
padding:5px 0;
}
input{
font:16px Verdana, sans-serif;
padding:5px;
}
h2{
font:26px Verdana, sans-serif;
color:#464544;
}
h2 a{
font:26px Verdana, sans-serif;
color:#464544;
}
</style>
</head>
<body>
<div class="allfonts">
<h2><a href="./">Выбор шрифта</a></h2>
<form action="./" method="get" enctype="text/plain">
<input name="name" type="text" value="" placeholder="Любое слово" />
<input type="submit" value="Посмотреть">
</form>
';
for($i=0;$i<count($fonts);$i++)
{
$font = explode(":",trim($fonts[$i]));
$fname = $font[0];
$file = $font[2];
echo "<p><img src='img.php?f=".$i."&s=".$name."'/><p>Шрифт: ".$fname." </p><HR>";
}
echo $end;
echo '</body>
</html>';
?>
img.php:
<?
$fonts = file('fonts.txt');
$fi = $_GET['f'];
$font = explode(":",$fonts[$fi]);
$inFontSize = $font[1];
$inFontFile = 'ttf/'.trim($font[2]);
$inText = $_GET['s'];
$bbox = imagettfbbox($inFontSize, 0, $inFontFile, $inText);
$width = abs($bbox[0]) + abs($bbox[2]);
$height = abs($bbox[5]) + abs($bbox[1]);
$x = abs($bbox[0]);
$y = abs($bbox[5]);
header('Content-type: image/png');
$img = imagecreatetruecolor($width, $height);
$text_colour = imagecolorallocate($img, $redColor, $greenColor, $blueColor);
$background = ImageColorAllocateAlpha($img, ($redColor == 255 ? 254 : $redColor + 1), ($greenColor == 255 ? 254 : $greenColor + 1), ($blueColor == 255 ? 254 : $blueColor + 1), 127);
imagefill($img, 0, 0, $background);
imagecolortransparent($img, $background);
myimagettftext($img, $inFontSize, 0, $x, $y, $text_colour, $inFontFile, $inText, $inAlign, $width, $height);
imageAlphaBlending($img, false);
imageSaveAlpha($img, true);
imagepng($img);
imagecolordeallocate($img, $text_colour);
imagecolordeallocate($img, $background);
imagedestroy($img);
function myimagettftext(&$image, $size, $angle, $left, $top, $color, $font, $text, $align, $width, $height) {
$text_lines = explode("\n", $text);
if ($align == ALIGN_LEFT || count($text_lines) <= 1) {
imagettftext($image, $size, $angle, $left, $top, $color, $font, $text);
} else {
$lines = array();
$line_widths = array();
$line_heights = array();
$line_ys = array();
$sum_height = 0;
foreach ($text_lines as $index => $block) {
$dimensions = imagettfbbox($size, $angle, $font, $block);
$line_width = abs($dimensions[0]) + abs($dimensions[2]);
$line_height = abs($dimensions[5]) + abs($dimensions[1]);
$line_y = abs($dimensions[5]);
$lines[$index] = $block;
$line_widths[$index] = $line_width;
$line_heights[$index] = $line_height;
$line_ys[$index] = $line_y;
$sum_height += $line_height;
}
$max_width = max($line_widths);
$max_width = $max_width + floor(($width - $max_width) / 2);
$delta_h = floor(($height - $sum_height) / (count($lines) - 1));
$top_offset = 0;
$left_offset = 0;
foreach ($lines as $index => $line) {
if ($align == ALIGN_CENTER) {
$left_offset = ($max_width - $line_widths[$index]) / 2;
} elseif ($align == ALIGN_RIGHT) {
$left_offset = ($max_width - $line_widths[$index]);
}
imagettftext($image, $size, $angle, $left_offset - $left, $line_ys[$index] + $top_offset, $color, $font, $line);
$top_offset += (isset($line_heights[$index]) ? $line_heights[$index] : 0) + $delta_h;
}
}
}
$redirect = isset($_SERVER['HTTP_REFERER'])? $_SERVER['HTTP_REFERER']:'redirect-form.html';
header("Location: $redirect");
exit();
?>
Ответы (2 шт):
Автор решения: Krya
→ Ссылка
Возможностей сделать это много. Например создать шорткод и его выводить в контенте там, где это надо.
В functions.php вашей темы добавьте:
add_shortcode('font-form', 'font_form_callback');
function font_form_callback(){
require ABSPATH . 'img/index.php';
}
Теперь в контенте(в админке) добавьте сам шорткод: [font-form]
Автор решения: SeVlad
→ Ссылка
Если это нужно только для одной страницы, то возможно лучше и проще будет создать свой шаблон с имеющимся кодом См https://developer.wordpress.org/themes/template-files-section/page-template-files/