PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Actions not firing, or not working

  1. gargleblaster
    Member

    This is my first attempt at WP programming, so there's no telling what I'm doing wrong. :-)

    First, I added
    include_once (TEMPLATEPATH . '/user-functions.php');
    into functions.php.

    I wasn't seeing anything from my user-functions.php code, so switched from include_once to a simple include
    include (TEMPLATEPATH . '/user-functions.php');
    thinking that this was an issue of not reloading my code changes, but it made no difference.

    I have all kinds of debugging echos in user-functions.php, but I'm not seeing anything. So, my first question is how do you do debugging on functions that happen before a page is rendered? I get the feeling my echos are being emitted too early in the request cycle, so they're being lost. What else can I do to see my debugging output?

    But I wouldn't be asking questions about debugging if my hooks were working as I expect. Here's my ugly code (originally based on some code I found in this forum):

    add_action("gform_post_submission", "post_submission_handler");
    function post_submission_handler($entry){
        global $wpdb;
    
        $results = $wpdb->get_results($wpdb->prepare("  SELECT l.*, field_number, value
                                FROM wp_rg_lead l
                                INNER JOIN wp_rg_lead_detail ld ON l.id = ld.lead_id
                                WHERE l.id=%d
                                ORDER BY  l.id, field_number", $entry["id"]));
    
    echo "<h6>Form id: " . $form["id"] . "</h6>";
    echo "<h6>Post type: " . $post_data["post_type"] . "</h6>";
    echo "<h6>Form id: " . $form->id . "</h6>";
    echo "<h6>Post type: " . $post_data->post_type . "</h6>";
    echo "<h6>Entry: " . $entry . "</h6>";
    echo var_dump($entry);
    echo print_r($entry);
    
        foreach($results as $result){
            echo "<hr/>Entry Id: " . $result->id . "<br/>";
            echo "Field Number: " . $result->field_number . "<br/>";
            echo "Field Value: " . $result->value . "<br/>";
        }
    }
    
    function insert_my_post($note) {
    global $user_ID;
     $new_post = array(
     'post_title' => 'My New Post ' . $note,
     'post_content' => 'Lorem ipsum dolor sit amet...',
     'post_status' => 'publish',
     'post_date' => date('Y-m-d H:i:s'),
     'post_author' => $user_ID,
     'post_type' => 'post',
     'post_category' => array(0) );
     $post_id = wp_insert_post($new_post);
    }
    
    add_filter("gform_post_data", "set_the_post_type");
    function set_the_post_type($post_data, $form){
    	//-----------------------------------------
    	//Replace "post_tag" with your taxonomy name
    	$custom_taxonomy = "post_tag";
    	//----------------------------------------
    
    	$tags = implode(",", $post_data["tags_input"]);
    	$post_data["tax_input"] = array($custom_taxonomy => $tags);
    	$post_data["tags_input"] = null;
    echo "<h6>Form id: " . $form["id"] . "</h6>";
    
        if($form["id"] == '2'){
            echo "<h6>Setting post_type to CPTTest</h6>";
            $post_data["post_type"] = "CPTTest";
            insert_my_post('2');
        }
    
        if($form["id"] == '3'){
            echo "<h6>Setting post_type to Review</h6>";
            $post_data["post_type"] = "Review";
            insert_my_post('3');
        }
    insert_my_post('ALL');
    
        return $post_data;
    }

    I can't tell if my hooks are running or not. Is there something obviously wrong in my code? I'm not seeing new posts created. It's like the form submission falls into a black hole. Thanks in advance for any help getting this working.

    Posted 13 years ago on Monday December 27, 2010 | Permalink
  2. First things first, let's make sure your user-functions.php is being loaded.

    Put this in your functions.php:

    echo 'functions.php'

    ...and this in your user-functions.php:

    echo 'user-functions.php'

    When you refresh your site, both echos should be visible. If the functions.php echo is visible and the user-functions.php echo is not then the path to the user-functions.php is probably not accurate. If you are using a child theme of a framework you will want to user STYLESHEETPATH rather than TEMPLATEPATH when including files.

    Let me know where this information gets you and we can pick up from there. :)

    Posted 13 years ago on Monday December 27, 2010 | Permalink
  3. gargleblaster
    Member

    Okay, I learned a couple of things...

    1. Both files are being reached, since I see this output in several places on the dashboard, including the top:
    functions.phpuser-functions.php

    2. On a probably unrelated note, my child theme may be a little funky. I am using WPMU DEV's Studio Child theme. When I switched frominclude_once (TEMPLATEPATH . '/user-functions.php'); to include_once (STYLESHEETPATH . '/user-functions.php');I got this error:

    Warning: Cannot modify header information - headers already sent by (output started at /home/admin/public_html/mytest.com/wp-content/themes/studio/functions.php:3) in /home/admin/public_html/mytest.com/wp-admin/theme-editor.php on line 89

    . And I noticed that my functions.php and user-functions.php files are located in /wp-content/themes/studio rather than /wp-content/themes/studio-child. At one point after editing the files in the Theme Editor, I saw an error about not being able to find the file .../studio-child/user-functions.php. It seems like there should be a child version of these files. Isn't that the point?

    Anyway, item 2 notwithstanding, my functions seem to be loading. Now what?

    Thanks,
    Lee

    Posted 13 years ago on Tuesday December 28, 2010 | Permalink
  4. First thing, I would recommend moving your user-functions.php file to your child theme and also call that file from your child themes functions.php. The more you keep contained in the child theme the easier it will be to keep track of.

    With that said, I would test your gform_post_submission hook function again. The gform_post_submission echos should display above the confirmation message assuming that the form you are testing is using the "text" type of confirmation (http://grab.by/86mg ).

    The gform_post_data hook function's echos will not be visible in the output as the function is processed before the page output begins.

    Posted 13 years ago on Tuesday December 28, 2010 | Permalink
  5. gargleblaster
    Member

    Well, I found the debugging culprit:

    ajax=true

    If you want to see debugging output on the page, you can't include that in the shortcode. (Underlined to help the next poor slob. :-)

    And I've moved functions.php and user-functions.php to the child theme directory.

    I've run into another bit of a puzzle, though. I have one form that seems to be calling set_the_post_type while the other one is not. They both use the "text" type of confirmation that you mentioned above. What else would cause one form to use that hook while another one bypasses it?

    Finally, I'm not clear on how to handle the form data to create the custom post type. Should it be done in gform_post_data or gform_post_submission? I'm too new to WP to know what's in the arguments, what's available in the database, etc. I see what's happening the code I swiped above, but I don't understand why it's done the way it's done.

    All I need -- for now -- is to take all the form data and convert it into a custom post type. Can you give me a quick example of the right way to do that?

    Many thanks,
    Lee

    Posted 13 years ago on Friday December 31, 2010 | Permalink
  6. What else would cause one form to use that hook while another one bypasses it

    Are both forms being used to create posts? If so, double check both forms and ensure that each form is using at least one post field. Any form that is being used to create a post will trigger the gform_post_data hook.

    To update the post type, you'll want to use the gform_post_data and do something like this:

    add_filter("gform_post_data", "change_post_type", 10, 2);
    function change_post_type($post_data, $form){
    
        //only change post type on form id 5
        if($form["id"] != 5)
            return $post_data;
    
        $post_data["post_type"] = "your-post-type";
        return $post_data;
    }
    Posted 13 years ago on Friday December 31, 2010 | Permalink
  7. gargleblaster
    Member

    Argh. That was it. It wasn't a post, because I hadn't included any Post Fields. That ought to be more clearly documented or shown in the UI.

    Will any Post Field do the trick?

    Many thanks!

    And a very Happy New Year!

    Posted 13 years ago on Friday December 31, 2010 | Permalink
  8. Yup, any 'ol form field will do it. :)

    Happy New Year!

    Posted 13 years ago on Friday December 31, 2010 | Permalink