N
N
Nikita2020-02-25 19:14:58
API
Nikita, 2020-02-25 19:14:58

How to send a 404 error source to a VK message via api?

There is an interesting idea that is now working, but it does not work completely smoothly. All this is implemented on wp with a custom 404 page. The code that below clings to the 404 page and sends a message to VK (the site runs a script that crashes from time to time (+ the plugin overwrites an important file and everything breaks (cancel the recording is not a variant), but operational there is no way - how to find out in time that the errors have gone)
// I immediately say in the code I don’t rummage at all - a dense forest)
Code

function wpd_do_stuff_on_404(){
    if( is_404() ){
           $url = 'https://api.vk.com/method/messages.send';
    $params = array(
        'user_id' => $id = '266212779',    // Кому отправляем, если взять вместо и вставить user_ids тогда можно будет добавлять id через запятую (сообщение будет отправляться 2х пользователям)
        'message' => $message = '<br>__ ▂ ▃ ▅ ▇ █ 。◕‿ ◕。 █ ▇ ▅ ▃ ▂ __<br>Привет! На сайте появилась ошибка 404<br>Проверь пожалуйста<br>[ ▇ ▃ ▄ ▅ █ ▇ ▂ ▃ ▁ ▄ ▅ █ ▅ ▃ ▇ ]',   // Что отправляем
        'access_token' => '0000000000000000000000000',  // access_token можно вбить хардкодом, если работа будет идти из под одного юзера
        'v' => '5.37',
    );

    // В $result вернется id отправленного сообщения
    $result = file_get_contents($url, false, stream_context_create(array(
        'http' => array(
            'method'  => 'POST',
            'header'  => 'Content-type: application/x-www-form-urlencoded',
            'content' => http_build_query($params)
        )
    )));
    }
}
add_action( 'template_redirect', 'wpd_do_stuff_on_404' );

Actually the question is how to pass a link to the source of the 404 ps error in the message field in the wp engine
so that the code works in the 404.php file this was added -
<?php echo do_shortcode( '  [wbcr_php_snippet id="1135"]' ); ?> (плагин добаления шорткодов, потому что как вывести в странице код ошибки я не знаю, а вот как добавить шорткод в php это я нагуглил)
<?php echo do_action('template_redirect'); ?>

the code was taken from some article on habré
, a hook was found to hook the 404 page (thanks to the chela from the toaster (habr questions)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alex, 2020-02-25
@otgamera

display the error code in the page I do not know

But find out. And do not fence such crutches with shortcodes.
<?php echo do_action('template_redirect'); ?>

This is completely off topic since the VI executes the template_redirect hook itself.
And the answer to your question is googled in 10 seconds:
https://stackoverflow.com/a/6768831
Get the requested URL and add it to the message text

N
Nikita, 2020-02-25
@otgamera

But <?php echo do_action('template_redirect'); ?>it was inserted there not by experience ... Namely, a code was taken that captures the 404 page (I had a question in my profile, like how to capture 404)
+ What will be the code that would make it all work?
What do you need to write to send?
Minimum code knowledge

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question