1

I have a front end editor set up for users to create posts and I would like to make it as simple as possible to create posts with the least amount of fields involved. Is it possible to generate the post title based on the first h2 element that appears in the content field? How would I do this?

  • How are you programmatically creating the posts now? And how are you getting the content field? – jgraup Dec 29 '15 at 17:32
  • What do you plan to do if there is no H2 element? If they change the H2 at some point after the post has been saved, do you want to update the title and slug also? Are you wanting to strip the first H2 from the content so it's not duplicated in the post content? Would it be easier to use CSS h2:first-of-type to style it in your templates so it appears as a title? Surely an additional Title field isn't that much of an inconvenience? – Howdy_McGee Dec 29 '15 at 17:33
  • Im using Gravity Forms to collect the post title in the first field, the main content in a WYSIWYG field, and then a dropdown field to select the category. Gravity Forms creates a post automatically upon submission of the form. – user304165 Dec 29 '15 at 17:56
1

If you can handle this in the PHP side, you might be able to pull the h2 tags from the content using regular expression.

PHP VERSION

Test content

$content = "<h2>This is H2 Content</h2><p>This is p content</p>Random Content<h2>This is another H2 section</h2><b><h2>This the third H2 section</h2></b>";

Grab any text inside H2s

preg_match_all('#<h2>(.*?)</h2>#', $content, $matches);

// Merge the first 2 matches

$potential_title = implode ( ' - ', array_slice($matches[0], 0, 2));

If we have something...

if( ! empty ( $potential_title ) ) {

Remove any HTML from the remaining content

    $cleaner_title = strip_tags( $potential_title );

Prep the post title update

    $my_post_updates = array(
        'ID'           => $post_id,
        'post_title'   => $cleaner_title, 
    );

    // Update the post into the database

    wp_update_post( $my_post_updates );
}

JS VERSION

The Javascript version of this is similar, you would just be required to intercept the form submission, grab the content, parse the results then pass the data along.

This just alerts the result of parsing test content.

// test content    
var content = "<h2>This is H2 Content</h2><p>This is PPP content</p>Random Content<h2>This is another H2 section</h2><b><h2>This the third H2 section</h2></b>";

// variables to store regEx results. 
var match, regex = /<h2>(.*?)<\/h2>/ig, matches = [];

// get all the matches and put in array
while (match = regex.exec(content)) { matches.push( match[1] ); }

// if we have any...
if( matches.length ) {

   // pull the first 2 and join them together
   var title = matches.slice ( 0, 2 ).join ( ' - ' );

   // send this along with your form submission
   alert(title);
}
  • Might I suggest: it may be easier to copy the code block if the notes are converted to PHP comments.......... // Comments or /** Comments **/ – Howdy_McGee Dec 29 '15 at 18:27
  • I agree, but then I'll get down voted for not explaining the code outside the code block... can't win around here. – jgraup Dec 29 '15 at 18:29
  • 1
    I should also add it's up for grabs on where this would take place so as a concept it's ~OK~ in this case to be separated because it's most likely the regEx would need to be on the JS side during form submission -IDK. Either way... I agree about copy/paste but I just framed THIS answer specifically NOT to be copy/paste. – jgraup Dec 29 '15 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.