• Hi,

    I found this code for adding a new term for a taxonomy from the front end.

    <?php
    
    // Check to see if correct form is being submitted, if so continue to process
    if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_term") {
    
        // Check to see if input field for new term is set
        if (isset ($_POST['term'])) {
    
            // If set, stores user input in variable
            $new_term =  $_POST['term'];
    
            // Function to handle inserting of new term into taxonomy
            wp_insert_term(
    
              // The term (user input)
              $new_term,
    
              // The club taxonomy
              'Club'        
    
            );
    
        } else { 
    
            // Else throw an error message
            echo 'Please enter a club name!';     
    
        }
    
    }
    
    ?>
    
    <form id="insert_term" name="insert_term" method="post" action=""> 
    
        <input type="text" value="" name="term" id="term" />
        <input type="submit" value="Add Club" id="submit" name="submit" />
        <input type="hidden" name="action" value="new_term" />
    
    </form>

    I need help working this into a page. How can I insert both PHP & HTML?

    Thanks in advance!

Viewing 1 replies (of 1 total)
  • Hello,

    This code is clearly called from a button, or a search page, plus it can’t be used as copy and paste.

    To work with taxonomy, you need to create some settings before calling anything like that (like in your theme functions.php). And quick answer is your term must be inside it – like animal term to call later dog or cat.

    To learn more about how to mix php and html in same page, there are many tutorials over the net, and mainly the codex (codex.wordpress.org) where you have many examples of how a theme is built, how to use taxonomies, so on.

    Also for your question, WordPress often mixes php and html, but at different levels. On a classic theme, low-level pages like header.php for a theme makes a lot of both but mainly html, functions.php is rather high-level with php only, and for instance a mid-level page called my-custom-page.php is in general 50% both.

    Finally, mixing PHP and HTML is not an easy task at first glance, a good php book is very useful for that too.

Viewing 1 replies (of 1 total)

The topic ‘Add taxonomy term code’ is closed to new replies.