Answer the question
In order to leave comments, you need to log in
WP plugin is executed multiple times, why?
Hello, we are making a plugin for personal use, at the moment it looks like this:
<?php
/*
Plugin Name: Test
Plugin URI: https://test.ru
Description: Уведомления о новых записях
Version: 0.0.2
Author: Test
Author URI: https://test.ru
*/
const VERSION = '0.0.2';
include "/home/client/vendor/autoload.php";
use Telegram\Bot\Api;
add_action( 'publish_post', 'test_my_update');
add_action ('admin_menu','test_renderMenu');
add_action( 'admin_notices', 'test_showError');
function test_my_update( $post_id) {
// If this is a revision, don't send the email.
if ( wp_is_post_revision( $post_id ) ) {
return;
}
$post = get_post( $post_id );
if ($post->post_type != "post") {
return;
} else {
$post_title = $post->post_title;
$post_content = $post->post_excerpt;
$post_uri = $post->guid;
$telegram = new Api(get_option ("test_telegram_token"));
$telegram->sendMessage ([
"chat_id" => get_option ("test_telegram_id"),
"text" => "На сайте что-то новенькое: {$post_title}\nПодробнее по ссылке:\n{$post_uri}",
"parse_mode" => "HTML"
]);
}
}
function test_renderMenu(){
add_options_page ('Уведомления о новых записях','Уведомления о новых записях',8,'notify','test_notifyRender');
}
function test_notifyRender(){
echo '<h1>Уведомления о новых записях</h1>';
add_option ('test_telegram_token','None');
add_option ('test_telegram_id','None');
add_option ('test_vk_token','None');
echo '<h2>Изменение настроек</h2>';
test_renderSettings();
test_renderFooter();
}
function test_renderFooter() {
echo "
<p>Данная версия плагина все еще в разработке!</p>
<p>Текущая версия: ".VERSION."</p>
<p><a href='mailto:[email protected]?subject=Плагин версии ".VERSION."'>Написать письмо</a></p>
";
}
function test_renderSettings() {
if (isset($_POST['test_opt_btn'])) {
if (function_exists ("current_user_can") && !current_user_can ("manage_options")) {
die(_e ("Ooops...","test"));
}
if (function_exists ("check_admin_referer")) {
check_admin_referer ("test_notify_settings_form");
}
$telegram_token = $_POST['test_telegram_token'];
$telegram_id = $_POST['test_telegram_id'];
update_option ("test_telegram_token",$telegram_token);
update_option ("test_telegram_id",$telegram_id);
}
$url = $_SERVER['PHP_SELF'].'?page=notify&updated=true';
echo '<p>Уведомления в Telegram</p>';
echo "<form name='test_notify_settings' method='post' action='{$url}'>";
if (function_exists ('wp_nonce_field')) {
wp_nonce_field ('test_notify_settings_form');
}
echo "
<p>
<label for='test_telegram_token'>Токен Telegram</label><br>
<input type='text' id='test_telegram_token' name='test_telegram_token' placeholder='Токен Telegram' value='".get_option ("test_telegram_token")."'>
</p>
<p>
<label for='test_telegram_id'>id Канала, беседы куда слать уведомления</label><br>
<input type='text' id='test_telegram_id' name='test_telegram_id' placeholder='id беседы Telegram' value='".get_option ("test_telegram_id")."'>
</p>
<p>
<input type='submit' value='Сохранить' name='test_opt_btn'>
</p>
";
echo '<form>';
}
function test_showError() {
if (get_option ("test_telegram_id") == null or get_option ("test_telegram_token") == null) {
echo "<div class='error notice'><p>Вы не указали необходимые настройки для уведомлений, пожалуйста перейдите <a href='/wp-admin/options-general.php?page=notify'>сюда</a> для настройки.</p></div>";
}
}
add_action( 'publish_post', 'test_my_update');
Answer the question
In order to leave comments, you need to log in
The solution to this question was the answer Denis Yanchevsky
Not sure what it is, but it might help. exile
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question