WordPress - 为什么我的自定义函数被调用两次?

Why my custom function being called twice in WordPress?

For instance i have this function in functions.php:

function your_function_name() {
 if(isset($_POST['your_var'])) {
   // return $error;
    echo '<p class="alert alert-danger"><span class="close" data-dismiss="alert">&times;</span>  Work NOT submitted! Please correct the error(s) below.</p>';
 }
}

My template:

<?php
your_function_name();
?>

<form class="form-submission" method="post" action="">

<input type="text" class="form-control" placeholder="Name" name="your_var">
...

When the form is submitted, I get this <p class="alert alert-danger"><span class="close" data-dismiss="alert">&times;</span> Work NOT submitted! Please correct the error(s) below.</p> twice on my screen. One is before the html tag and other one is on the correct location:

<p class="alert alert-danger"><span class="close" data-dismiss="alert">&times;</span>  Work NOT submitted! Please correct the error(s) below.</p><!DOCTYPE html>
<html lang="en-US" class="no-js">
<head>
....

<p class="alert alert-danger"><span class="close" data-dismiss="alert">&times;</span>  Work NOT submitted! Please correct the error(s) below.</p>

<form class="form-submission" method="post" action="">

<input type="text" class="form-control" placeholder="Name" name="your_var">
...

The first one before the html tag should not exist but it does! Why? How can I fix this?

I want the message appear before the form tag not before the html tag.

<?php
   if(isset($_POST['submitbuttonname']))
      your_function_name();
?>

try this maybe this works for you. In your code the function is called two times, first when page is loaded and another when you call it. So give the condition that if the form is submitted then call that function.

Following this guide, just put my code before:

<?php
get_header(); ?>

Then the problem solved.