• Resolved jonobr1

    (@jonobr1)


    This post is similar to this post which has yet to be resolved as well.

    So I’ve been messing around with the theme p2. Frankly, its awesome. I can’t live without it…haha. I’ve been looking around for a few days now how to resolve this problem but I’m not entirely how to approach this.

    In post-form.php I’d like to add a field that refers directly to the custom field music_src I’ve setup within the WordPress administration posts. I’ve looked through the codex and there are only functions to return the meta data, i.e. get_post_meta($ID, $key ) nothing similar to writing like post_meta( $key, 'value' ). I’d write a function myself that would write to the database, but I’d rather try to work with how p2 is inherently setup because I don’t fully understand how the jquery/ajax handles the information from post-form.php

    It seems to be the method at function newPost() in p2.js but I don’t understand how the arguments are written and how that translates into an insertion into the database. Particularly:

    var tags = $('#tags').val();
    		if (tags == p2txt.tagit) tags = '';
    		var args = {action: 'prologue_new_post', _ajax_post:nonce, posttext: posttext, tags: tags};
    		var errorMessage = '';
    		$.ajax({
    			type: "POST",
    			url: ajaxUrl,
    			data: args,
    			success: function(result) {
    				if ("0" == result)
    					errorMessage = p2txt.not_posted_error;
    
    				$('#posttext').val('');
    				$('#tags').val(p2txt.tagit);
    				if(errorMessage != '')
    					newNotification(errorMessage);
    
    				if ($.suggest)
    					$('ul.ac_results').css('display', 'none'); // Hide tag suggestion box if displayed
    
    				if (isFirstFrontPage && result != "0") {
    					getPosts(false);
    				} else if (!isFirstFrontPage && result != "0") {
    					newNotification(p2txt.update_posted);
    				}
    				submitProgress.fadeOut();
    				thisFormElements.attr('disabled', false);
    				thisFormElements.removeClass('disabled');
    			  }
    		});

    I don’t know what unewpost is or how var args works

    Does anyone else have desires to do this, resolutions, issues?

Viewing 15 replies - 1 through 15 (of 15 total)
  • Check out functions.php and prologue_new_post() and look for how the xhr/ajax request is being made. That should lead you in the right direction.

    Thread Starter jonobr1

    (@jonobr1)

    Thanks Noel, I’ll be looking into it and keeping you posted on my findings.!

    Thread Starter jonobr1

    (@jonobr1)

    okay, so I understand how the functions.php and prologue_new_post() are working I think. I modified them in the way that i wanted to. I noticed that for the most part the php is working smoothly. I happily switched over to p2.js to see how the ajax was handling these particular bits of information because when I try to use the post-form I get an error message that p2 has built in when the result in the $.ajax comes up 0. I’ve narrowed it down to the var args. I just don’t understand how this works. I looked it up and it looks to be a JSON array carrying all the objects needed to post to the databse, but I can’t simply drop my variables into this array. How are these JSON objects made and declared?

    var args = {action: 'prologue_new_post', _ajax_post:nonce, <em>posttext: posttext, tags: tags</em>};

    I think if I understood this and was able to replace the posttext and tags lik:

    var myvar = string from the form;
    
    var args = {action: 'prologue_new_post', _ajax_post:nonce, <em>myObject:myvar</em>};

    This returns an error. Looking through JSON it seemed like the variable args was the point in the javascript where php was turned into usable javascript data so I tried pulling my form titles as the second object like so:

    //—In functions.php
    $theTitle = $_POST['title'];
    
    //—In p2.js
    var myVar = string from the form;
    
    var args = {action: 'prologue_new_post', _ajax_post:nonce, <em>myVart:title</em>};

    I also tried 'title' to see if it were supposed to be a string but still errors. The problem seems to be that the array as a whole and all the individual arguments in the array return undefined, but I don’t really understand that line very well…

    Any clarification on how this array holds its information would be greatly appreciated. Thanks!

    Thread Starter jonobr1

    (@jonobr1)

    no JSON or ajax p2-ers out there ???

    Hey, I want to know about this too!

    Thread Starter jonobr1

    (@jonobr1)

    Transcription of a little visual I made when I realized what I was doing wrong
    https://www.flickr.com/photos/jonobr1/3586725313/

    What I didn’t get was the order of operations. In hindsight this is a no-brainer which is why no one probably commented. Nonetheless this is paramount to getting this script to run properly. There are two things that I ran into that were troublesome—

    1. p2.js handles the form. So it comes BEFORE functions.php handles the information. The pixy dust that is JSON turns your variables in p2.js into $_POST[] item that is retrievable in functions.php

    2. The meta data is on entirely different TABLE, meaning that when you edit functions.php you can use the same function wp_insert_post() you need to have a separate function add_post_meta() —this seems like something the wordpress team should address, but whatever. It’s known now so I guess I can deal with it.

    var args = {action: ‘prologue_new_post’, _ajax_post:nonce, post_title: post_title_src, meta_value: post_music_src}; //line 176

    meta_value is your key, which means that is what your $_POST[‘key‘] variable is looking for in functions.php!!!
    post_music_src is your javascript variable which holds the text data from your Form in post_form.php!!!

    $post_id = wp_insert_post( array(
    		‘post_author’		=> $user_id,
    		‘post_title’		=> $post_title_src,
    		‘post_status’		=> ‘publish’
    	) );								// line 544
    	add_post_meta($post_id, ‘music_src’, $post_music_src);

    functions.php works the same way. the pink are your keys and the blue are your values.
    The big difference is that add_post_meta is on a separate table and it takes 3 arguments—
    1. the id to which the meta is attached 2. the key or the name for the type of meta data + 3. the value or data that the key contains.

    Soon to be up— https://glitzypoppyhooray.com/

    I’d like to see this in action, but your site requires a login to post a reply. Could you post a screenshot of what the reply field looks like? In fact, I’m working on a site that could really use this feature. Hopefully, you’ll share more info!

    Thread Starter jonobr1

    (@jonobr1)

    @jpelker, definitely definitely. If you dm me on twitter with your email address I can send you an invite. Otherwise I hope these pics help/I’ve uploaded a .zip document with the three files the form, the functions, and the ajax which you can download.

    IMG | ZIP (the zipped file is the entire GPH theme)

    Also the code is here below:

    //–Form

    <?php
    $user = get_userdata( $current_user->ID );
    $name = isset( $user->first_name ) && $user->first_name? $user->first_name : $user->display_name;
    ?>
     <div id="postbox">
    	<form id="new_post" name="new_post" method="post" action="<?php bloginfo( 'url' ); ?>/">
    		<input type="hidden" name="action" value="post" />
    		<?php wp_nonce_field( 'new-post' ); ?>
    		<div class="inputarea">
    			<label for="post_title_src"><?php printf( __('Hey, what&apos;s Glitzy Poppy Hooray today?', 'p2'), wp_specialchars( $name ) ) ?></label>
    			<label class="post-error" for="posttext" id="posttext_error"></label>
    			<div class = "prepostcat">
    				<div class="postrow">
    					<input type="text" name="post_title_src" id="post_title_src" tabindex="1" autocomplete="off" value="<?php echo attribute_escape(__('Artist —?Song', 'p2')); ?>"
    						onfocus="this.value=(this.value=='<?php echo js_escape(__('Artist —?Song', 'p2')); ?>') ? '' : this.value;"
    						onblur="this.value=(this.value=='') ? '<?php echo js_escape(__('Artist —?Song', 'p2')); ?>' : this.value;" />
    				</div>
    				<br />
    				<div class="postrow">
    						<input type="text" name="post_music_src" id="post_music_src" tabindex="2" autocomplete="off" value="<?php echo attribute_escape(__('https://someplace.com/endingwith.mp3', 'p2')); ?>"
    							onfocus="this.value=(this.value=='<?php echo js_escape(__('https://someplace.com/endingwith.mp3', 'p2')); ?>') ? '' : this.value;"
    							onblur="this.value=(this.value=='') ? '<?php echo js_escape(__('https://someplace.com/endingwith.mp3', 'p2')); ?>' : this.value;" />
    				</div>
    			</div>
    
    			<div class = "postcat">
    					<label for= "post_category">Choose a category —</label>
    					<!-- <input id = "post_category" type = "radio" name = "post_category" class="post_category" value="3">&nbsp;Glitzy&nbsp;
    					<input id = "post_category" type = "radio" name = "post_category" class="post_category" value="4">&nbsp;Poppy&nbsp;
    					<input id = "post_category" type = "radio" name = "post_category" class="post_category" value="5">&nbsp;Hooray -->
    					<?php wp_dropdown_categories('hide_empty=0&name=post_category'); ?>
    			</div><div class="clear"></div><br />
    			<div class = "postrow">
    				<input type="text" name="tags" id="tags" tabindex="3" autocomplete="off" value="<?php echo attribute_escape(__('Tag it', 'p2')); ?>"
    					onfocus="this.value=(this.value=='<?php echo js_escape(__('Tag it', 'p2')); ?>') ? '' : this.value;"
    					onblur="this.value=(this.value=='') ? '<?php echo js_escape(__('Tag it', 'p2')); ?>' : this.value;" />
    					<input id="submit" type="submit" tabindex="4" value="<?php echo attribute_escape(__('Post it', 'p2')); ?>" />
    			</div>
    			<span class="progress" id="ajaxActivity"><img src="<?php bloginfo('template_directory'); ?>/i/indicator.gif"
    				alt="<?php echo attribute_escape(__('Loading...', 'p2')); ?>" title='<?php echo attribute_escape(__('Loading...', 'p2')); ?>'/></span>
    		</div><div class="clear"></div>
    	</form>
    </div><!-- // postbox -->

    //–Ajax

    function newPost(trigger) {
    		var thisForm = $(trigger.target);
    		var thisFormElements = $('#post_title_src, #post_music_src, #post_category, #tags, :input',thisForm).not('input[type=hidden]');
    
    		var submitProgress = thisForm.find('span.progress');
    
    		//var posttext = $.trim($('#posttext').val());
    		var post_title_src = $.trim($('#post_title_src').val());
    		var post_music_src = $.trim($('#post_music_src').val());
    		console.log(post_music_src)
    		if ("" == post_title_src || "" == post_music_src) {
    			$("label#posttext_error").text('This field is required').show().focus();
    			return false;
    		}
    		toggleUpdates('unewposts');
    		if (typeof ajaxCheckPosts != "undefined")
    			ajaxCheckPosts.abort();
    		$("label#posttext_error").hide();
    		thisFormElements.attr('disabled', true);
    		thisFormElements.addClass('disabled');
    
    		submitProgress.show();
    		var tags = $('#tags').val();
    		if (tags == p2txt.tagit) tags = '';
    		var post_category = $('#post_category').val();
    		//var args = {action: 'prologue_new_post', _ajax_post:nonce, posttext: posttext, tags: tags};
    		var args = {action: 'prologue_new_post', _ajax_post:nonce, post_title: post_title_src, meta_value: post_music_src, post_category: post_category, tags: tags};
    		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    		console.log(args);
    
    		var errorMessage = '';
    		$.ajax({
    			type: "POST",
    			url: ajaxUrl,
    			data: args,
    			success: function(result) {
    				if ("0" == result)
    					errorMessage = p2txt.not_posted_error;
    
    				//$('#posttext').val('');
    
    				$('#post_music_src').val('');
    				$('#post_title_src').val('');
    
    				$('#tags').val(p2txt.tagit);
    
    				if(errorMessage != '') {
    					newNotification(errorMessage);
    				}
    				if ($.suggest)
    					$('ul.ac_results').css('display', 'none'); // Hide tag suggestion box if displayed
    
    				if (isFirstFrontPage && result != "0") {
    					getPosts(false);
    				} else if (!isFirstFrontPage && result != "0") {
    					newNotification(p2txt.update_posted);
    					console.log(p2.txt.update_posted);
    				}
    				submitProgress.fadeOut();
    				thisFormElements.attr('disabled', false);
    				thisFormElements.removeClass('disabled');
    			  }
    		});
    		thisFormElements.blur();
    		toggleUpdates('unewposts');
    	}

    //–Functions

    function prologue_new_post() {
    	if( 'POST' != $_SERVER['REQUEST_METHOD'] || empty( $_POST['action'] ) || $_POST['action'] != 'prologue_new_post' ) {
    	    die();
    	}
    	if ( !is_user_logged_in() ) {
    		die('<p>'.__('Error: not logged in.', 'p2').'</p>');
    	}
    	if( !current_user_can( 'publish_posts' ) ) {
    		die('<p>'.__('Error: not allowed to post.', 'p2').'</p>');
    	}
    	check_ajax_referer( 'ajaxnonce', '_ajax_post' );
    	$user_id		= $current_user->user_id;
    	//$post_content	= $_POST['posttext'];
    	$tags			= trim( $_POST['tags'] );
    	$category 		= $_POST['post_category'];
    	$post_title_src 		= $_POST['post_title'];
    	$post_music_src			= $_POST['meta_value'];
    
    	if ( $tags == __('Tag it', 'p2') || $tags == 'Tag it' ) $tags = '';
    	if ( $post_title_src == __('Artist —?Song', 'p2') || $tags == 'Artist —?Song' ) die('<p>'.__('Error: You must fill this in.', 'p2').'</p>');
    	if ( $post_music_src == __('https://someplace.com/endingwith.mp3', 'p2') || $tags == 'https://someplace.com/endingwith.mp3' ) die('<p>'.__('Error: You must fill this in.', 'p2').'</p>');
    
        $post_title = prologue_title_from_content( $post_content );
    
    	$post_id = wp_insert_post( array(
    		'post_author'	=> $user_id,
    		'post_title'	=> $post_title_src,
    		//'music_src'		=> $post_music_src,
    		//'post_content'	=> $post_content,
    		'post_category' => array($category),
    		'tags_input'	=> $tags,
    		'post_status'	=> 'publish'
    	) );
    
    	add_post_meta($post_id, 'music_src', $post_music_src);
    
    	echo $post_id? $post_id : '0';
    	exit;
    }

    These are the relevant bits from the files. Hope that helps

    @jonobr1: That is about the most helpful reply I’ve ever seen. Wow. Thank you so much.

    I feel like I almost have it solved, but I’m not having any luck getting my custom field (user_name) to register. I’ve also lost the ability for the content to become the post’s title. If anyone is interested in helping me spot these errors, please take a look at the code included in this Zipped P2 file.

    I only really only edited gph-form.php, functions.php, entry.php, P2.js and style.css files. If it’ll help, feel free to compare it to the zipped theme posted above from @jonobr1. I thought I’d have a better chance reverse engineering the 1.0.3 code borrowed from him than translating it to my P2 1.0.4 code.

    Thank you in advance.

    Thread Starter jonobr1

    (@jonobr1)

    hmmm, I haven’t looked at your code extensively yet, but just from what you’ve written there’s two things that come to mind that may help.

    1. The packaged .zip available doesn’t use entry.php it’s just there for reference for when I break gph-form.php

    2. Your post title issue; does that not insert into the db or does it not show up in ajax? At the end of newPost you’ll notice a case change for unewposts this inserts the new post via ajax. functions.php and unewposts are 2 concurrent functions because functions actually gets it in the database + unewposts shows the content that you’ve submitted via ajax.

    Also when you modify prologue_new_post in functions.php don’t forget to update prologue_new_post_noajax — I’ll get back to you when I have more time to further investigate.

    I’m getting close…

    Here’s my code, if anyone wants to take a look at it: Zipped theme.

    The custom field is posting (at least after refresh) and the title and categories do get added to the post’s database entry. Good, good.

    Unfortunately, the new entries don’t read quite right yet. See here for what it looks like immediately after posting. Then, after a refresh, they only say “Click to edit” (Image here).

    Any clues? What file/lines should I be looking to edit?

    And it doesn’t work in firefox…

    Thread Starter jonobr1

    (@jonobr1)

    What doesn’t look right about the first image??? I modified entry.php a bit. If you look around lines 29-38

    —————————————-

    Also I used the following plugins—
    Akismet, All in One SEO Pack, Audio player, Invite friends, Peter’s Login Redirect, and Twitter Tools

    —————————————-

    The custom field is posting (at least after refresh)

    You have to update function prologue_latest_posts() in functions.php around line 703

    Hope that helps! Sorry I can’t fix for you, but it’s good practice ??

    There’s no post content, just a title–ha!

    How could I make the post title and the post content the same? I’m cheating right now by posting the title, but I’d rather have a cleaner solution.

    One issue remains:

    1) Firefox doesn’t want anything to do with the post. Please see the site here: https://yourstatussucks.com/home/ . Safari works, though.

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘p2 Custom Field//Meta Data Insertion’ is closed to new replies.