Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在搜索表单中插入范围?(创世记)_Php_Wordpress_Search_Genesis_Search Form - Fatal编程技术网

Php 如何在搜索表单中插入范围?(创世记)

Php 如何在搜索表单中插入范围?(创世记),php,wordpress,search,genesis,search-form,Php,Wordpress,Search,Genesis,Search Form,当我呼叫时,它输出: <form class="search-form"> <meta itemprop="target"> <input type="search"> <input type="submit"> </form> 正在寻找一种更安全的替代方案: add_filter( 'genesis_search_form', 'my_search_button' ); function my_search_butto

当我呼叫时,它输出:

<form class="search-form">
  <meta itemprop="target">
  <input type="search">
  <input type="submit">
</form>
正在寻找一种更安全的替代方案:

add_filter( 'genesis_search_form', 'my_search_button' ); 
function my_search_button( $form ) {
    return str_replace( 
        '<input type="submit"', 
        '<span class="submit-icon"></span><input type="submit"', 
        $form 
    );
}
add_filter('genesis_search_form','my_search_button');
功能我的搜索按钮($form){
返回stru_替换(

“如果允许您对函数进行更改,请进行更改。如果不允许,请进行破解

$('form.search-form input[type=search]').after('<span class="submit-icon"></span>');
$('form.search-form input[type=search]')。在('')之后;

使用DOMDocument!以下是代码的工作版本:


如果您对DOMDocument没意见,这个版本更适合HTML5

add_filter( 'genesis_search_form', 'my_search_button' );
function my_search_button($form) {

    $document = new DOMDocument();
    $document->loadHTML($form);
    $xpath = new DOMXPath($document);
    $input = $xpath->query('//input[@type="submit"]');
    $span = $document->createElement('span');
    $span->setAttribute('class', 'sb-icon');

    if ($input->length > 0) {
        $input->item(0)->parentNode->insertBefore($span, $input->item(0));
    }

    $document->removeChild($document->doctype); 
    $document->replaceChild($document->firstChild->firstChild->firstChild, $document->firstChild);
    $form_html = $document->saveHTML();

    return $form_html;
}

只要函数不提供这样做的功能,您就无法做到这一点,如果允许它分叉函数并根据您的需要自定义它,您可以这样做。感谢您的回答,
jQuery
确实更简单,但我们正在
php
服务器端寻找解决方案。太棒了;它让您保持它是服务器端的,就像他想要的那样。以某种方式缓存结果也可能是有意义的,这样他就不必在每个请求中操作DOM块。
<?php

// Create a DOM Document
$dom = new DomDocument();

// Load your HTML
$dom->loadHTML('<form class="search-form">  
    <meta itemprop="target">
    <input type="search">
    <input type="submit">
</form>');

// Create a new <span>
$span = $dom->createElement('span', 'hello');

// Grab the <input elements (we dont have an ID)
$inputs  = $dom->getElementsByTagName('input');

// Add the <span> between the inputs 
$inputs->item(0)->parentNode->insertBefore($span, $inputs->item(1));

// By default when you loadHTML(), it generates doctype, html, head, and     body tags. remove them!
$dom->removeChild($dom->doctype);
$dom->replaceChild($dom->firstChild->firstChild->firstChild, $dom->firstChild);

// Finally get the HTML
$html = $dom->saveHTML();

// And output / return / whatever
echo $html;
add_filter( 'genesis_search_form', 'my_search_button' );
function my_search_button($form) {

    $document = new DOMDocument();
    $document->loadHTML($form);
    $xpath = new DOMXPath($document);
    $input = $xpath->query('//input[@type="submit"]');
    $span = $document->createElement('span');
    $span->setAttribute('class', 'sb-icon');

    if ($input->length > 0) {
        $input->item(0)->parentNode->insertBefore($span, $input->item(0));
    }

    $document->removeChild($document->doctype); 
    $document->replaceChild($document->firstChild->firstChild->firstChild, $document->firstChild);
    $form_html = $document->saveHTML();

    return $form_html;
}