Как интегрировать php код в плагин для wordpress
Первый раз делаю плагин для wordpress. Пока на начальном этапе, но надо уже интегрировать код на страницу как плагин, как это можно сделать? Как подготовить страницу к этому(там только выбор дизайна и прочее). Суть кода определить занятость домена. Вот код, как можно сделать плагином, что нужно писать или добавить? Помимо этой части
<?php
/*
Plugin Name: Название плагина
Plugin URI: http://страница_с_описанием_плагина_и_его_обновлений
Description: Краткое описание плагина.
Version: Номер версии плагина, например: 1.0
Author: Имя автора плагина
Author URI: http://страница_автора_плагина
*/
?>
Вот сам код на php <?php
function check_domain()
{
$host = htmlspecialchars(trim($_POST['name']));
$json = file_get_contents('http://ip-api.com/json/' . $host . '?lang=ru');
$array = json_decode($json, TRUE);
if (strcasecmp($array['status'], 'fail') == 0)
echo "<h5>Домен"." ".$host." "."свободен</h5>";
else
echo "<h5>Домен"." ".$host." "."зарегистрирован</h5>";
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h2><center>Проверка домена на занятость</center</h2>
<form action="#" method="post">
<label for="name">
<input type="text" name="name" placeholder="введите Домен">
<input type="submit" value="отправить">
</label>
</form>
<?php
if (isset($_POST['name']) && !empty(trim($_POST['name']))) {
check_domain();
}
?>
</body>
</html>
Ответы (1 шт):
Автор решения: KAGG Design
→ Ссылка
Вот полный код плагина с комментариями. Код протестирован.
<?php
/**
* Plugin Name: Название плагина
* Plugin URI: http://страница_с_описанием_плагина_и_его_обновлений
* Description: Краткое описание плагина.
* Version: Номер версии плагина, например: 1.0
* Author: Имя автора плагина
* Author URI: http://страница_автора_плагина
*/
/**
* Check domain.
*
* @param string $domain_name Domain name.
*
* @return string
*/
function check_domain( $domain_name ) {
$host = htmlspecialchars( trim( $domain_name ) );
$json = file_get_contents( 'http://ip-api.com/json/' . $host . '?lang=ru' );
$array = json_decode( $json, true );
if ( strcasecmp( $array['status'], 'fail' ) === 0 ) {
return '<h5>Домен ' . $host . ' свободен</h5>';
}
return '<h5>Домен ' . $host . ' зарегистрирован</h5>';
}
/**
* Domain shortcode.
*
* @return false|string
*/
function check_domain_shortcode() {
// We have to return string, so wrap html into output buffer functions.
ob_start();
?>
<h2>Проверка домена на занятость</h2>
<form method="post">
<label for="domain_name">
<input type="text" name="domain_name" placeholder="Введите домен">
<input type="submit" value="отправить">
</label>
</form>
<?php
return ob_get_clean();
}
add_shortcode( 'check_domain', 'check_domain_shortcode' );
/**
* Process domain page.
*
* @param string $content Post content.
*
* @return mixed|string
*/
function domain_page( $content ) {
if ( has_shortcode( $content, 'check_domain' ) ) {
// Do not use 'name' POST var in WordPress. It is reserved.
if ( isset( $_POST['domain_name'] ) ) {
$domain_name = filter_input( INPUT_POST, 'domain_name', FILTER_SANITIZE_STRING );
// Add the result of checking below the form.
return $content . check_domain( $domain_name );
}
}
return $content;
}
// Add processing of the content. Priority 0 to work before conversion of shortcodes.
add_action( 'the_content', 'domain_page', 0 );
Активируйте плагин и добавьте на страницу или в пост шорткод [check_domain]. Шорткод выведет форму, а код по хуку the_content - проверит статус домена и добавит результат ниже формы.