2012-03-02 73 views
2

我必须做一个简单的HTML表单,做一个简单的计算wordpress插件。这是我的表单代码。wordpress插件需要

<html> 
<head> 
    <title>Calculate</title> 
    <script language="javascript"> 
      function addNumbers() 
      { 
        var val1 = parseInt(document.getElementById("value1").value); 
        var val2 = parseInt(document.getElementById("value2").value); 
        var ansD = document.getElementById("answer"); 
        ansD.value = val1 + val2; 
      } 
    </script> 

</head> 
<body> 
    <input type="text" id="value1" name="value1" value="1"/> 
    <input type="text" id="value2" name="value2" value="2"/> 
    <input type="button" name="Sumbit" value="Click here"    
    onclick="javascript:addNumbers()"/> 
    <input type="text" id="answer" name="answer" value=""/> 
</body> 
</html> 

感谢

回答

0

我会说这更是一个设计任务比单纯的编码任务。

当涉及到在WordPress生成的页面上的某处输出您的代码时,我会建议使用wp_enqueue_script()来包含您的javascript。如果您确实需要内联脚本代码,则可以使用wp_headaction。要打印实际的form元素,on选项将挂接到the_contentfilter,并仅添加/预先添加内容。其他选项包括创建shortcode以允许用户在页面内容中插入[my_form],或者创建一个template tag以包含在主题模板文件中。

但所有这一切都取决于您的用户的需求以及您打算使用此插件完成的任务。这就是说,它肯定是在WordPress的插件的概念,至少基本的了解具有Ersel阿克尔的建议,虽然插件做你问什么,或者可能是一样简单一件好事:

// Enqueue javascript (placed in plugins.js in js subdir of plugin) 
wp_enqueue_script('plugin.js.handle', plugins_url('js/plugin.js', __FILE__), array()); 

// Add filter to the_content 
add_action('the_content', 'my_plugin_content'); 

// Append form to page content under certain conditions 
function my_plugin_content($content) { 

    $form = '<input type="text" id="value1" name="value1" value="1"/> <input type="text" id="value2" name="value2" value="2"/> <input type="button" name="Sumbit" value="Click here" onclick="javascript:addNumbers()"/> <input type="text" id="answer" name="answer" value=""/>'; 

    if (some_magic_conditions_are_met()) { 
     return $content . $form; 
    } 

    return $content; 
} 
相关问题