Как изменить action у формы поиска в wordpress?

Пользователь

от vladimir , в категории: PHP , год назад

Как изменить action у формы поиска в wordpress?

Facebook Vk Ok Twitter LinkedIn Telegram Whatsapp

2 ответа

Пользователь

от dan_farrell , год назад

@vladimir 

To change the action of the search form in WordPress, you can use the get_search_form filter hook in your theme's functions.php file. Here's an example code snippet that demonstrates how to change the action URL to a custom URL:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
function change_search_form_action( $form ) {
    $form = '<form role="search" method="get" id="searchform" action="http://example.com/custom-search-page/">
    <label class="screen-reader-text" for="s">' . __('Search for:') . '</label>
    <input type="text" value="' . get_search_query() . '" name="s" id="s" />
    <input type="submit" id="searchsubmit" value="'. esc_attr__('Search') .'" />
    </form>';

    return $form;
}
add_filter( 'get_search_form', 'change_search_form_action' );


In the above code, we use the get_search_form filter hook to modify the HTML output of the search form. We replace the action attribute of the <form> element with our custom URL, which in this case is http://example.com/custom-search-page/. You can replace this URL with your own custom search page URL.


After adding this code to your theme's functions.php file, the search form in your WordPress site will use the new custom action URL that you specified.

Пользователь

от nelle , 2 месяца назад

@vladimir 

Для изменения action формы поиска в WordPress можно использовать хук фильтра get_search_form в файле functions.php вашей темы. Вот пример кода, демонстрирующий, как изменить URL действия на пользовательский URL:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
function change_search_form_action( $form ) {
    $form = '<form role="search" method="get" id="searchform" action="http://example.com/custom-search-page/">
    <label class="screen-reader-text" for="s">' . __('Search for:') . '</label>
    <input type="text" value="' . get_search_query() . '" name="s" id="s" />
    <input type="submit" id="searchsubmit" value="'. esc_attr__('Search') .'" />
    </form>';

    return $form;
}
add_filter( 'get_search_form', 'change_search_form_action' );


В приведенном выше коде мы используем хук фильтра get_search_form, чтобы изменить HTML-вывод формы поиска. Мы заменяем атрибут действия элемента на наш пользовательский URL, в данном случае http://example.com/custom-search-page/. Вы можете заменить этот URL на свой собственный URL страницы поиска.


После добавления этого кода в файл functions.php вашей темы, форма поиска на вашем сайте WordPress будет использовать новый пользовательский URL действия, который вы указали.