Forum Replies Created
-
Alright solved again. Turns out there was a mistake in my code. Here’s the breakdown:
Old Code:
`function cg_postcatagory( $post_id = false )
{
if ( empty( $post_id ) || empty( $_POST ) )
return false;$post_id = (int) $post_id;
$postmeta = $_POST;$cg_newmeta = ( cg_newmeta( $post_id, $postmeta ) ) ? true : false;
return $cg_newmeta;
}
add_action( ‘bp_forums_new_post’, ‘cg_postcatagory’ );`Initially I wanted the post to store custom metadata, but that turned out to be a pain in the neck to use, and unnecessarily complicated. So, I decided to store tags instead. The problem, I discovered, was that the function was using the post_id to store the tags, which apparently isn’t how it’s supposed to work. What I should have one was instead topic_id instead, since that’s what tags are attached to (according to the bb_terms_relationships table).
So I modified my function accordingly and ended up with
`function cg_postcatagory( $topic_id = false )
{
if ( empty( $topic_id ) || empty( $_POST ) )
return false;$topic_id = (int) $topic_id;
$postmeta = $_POST;$cg_newmeta = ( cg_newmeta( $topic_id, $postmeta ) ) ? true : false;
return $cg_newmeta;
}
add_action( ‘bp_forums_new_topic’, ‘cg_postcatagory’ );`And it worked, testing it on the latest stable WordPress (3.2.1) and BuddyPress (1.2.9) and so far so good. If the code is solid, I hope somebody benefits from this. if It isn’t, I hope somebody can point out the holes in it, so I can plug them up.
Will set the status of this thread to Resolved (permanently this time, I hope)
I’m still new to BuddyPress, so this may not be the best approach, but here’s what I’ve done:
1. Copy bp-default > groups > index.php into your own child-theme (make sure you keep the same directory structure)
2. Open the newly copied groups > index.php that is located in your child-theme
3. Find: “
4. Edit it by adding is_super_admin() to the if expression so it’s “ This is to check to see if user is logged in AND is a super_admin. If true then display the “create group button” if false then don’t.I found something close with the default theme’s activity page. Still searching for the tags, though. I totally accidentally the buddypress theme
@r-a-y Interesting to note, I’ve just tried the code, and it works. But for some reason, while it works flawlessly on my XAMPP box (Windows Vista), my MAMP box will not load any of the forums or forum posts… It just loads a blank page. I just hope it works on my live server…
EDIT: Works on my live server. So my MAMP must have something screwy with its settings. Ahh no big, it’s not my main work computer, yet…
Thanks for the tip! I’m going to have to figure out how to validate the form input to ensure that it’s an array (and that the check-boxes have been selected). That’s next on my to-do list after I’ve set up some custom loops to display posts based on the metadata I’ve created (will post the code when I’m done, so all can critique, and benefit)
Yes, I have tried r-a-y’s suggestion, and it worked like a charm (Thanks again r-a-y!) However, using bb_update_postmeta() wouldn’t allow me to assign an object_type, as far as I could tell, by passing it as an argument. I’m not even sure if a custom object_type is necessary, but I figured it couldn’t hurt, and could prove useful later on… I’d love to tinker more with this function, but I have deadlines to meet so I’ll push on ahead and review this later, once I’ve got more time. For now, it gets the job done
EDIT: Function, r-a-y style!
`function cg_newmeta( $id, $postcat) {
do_action (‘bbpress_init’);
bb_update_postmeta($id, ‘cg_catagory’, $postcat);
}`my tweaks, to get it accepting arrays. Sound code?
`function cg_newmeta( $id, $postcat) {
global $catagory, $wpdb, $bbdb;
do_action( ‘bbpress_init’ );
foreach ($postcat as $value)
{
$wpdb->query( $wpdb->prepare( “INSERT INTO $bbdb->meta ( meta_id, object_type, object_id, meta_key, meta_value ) VALUES (%d, %s, %d, %s, %s )”, $catagory->meta_id, ‘bb_cgcat’, $id, ‘cg_catagory’, $value ) );
}
}`
r-a-y! Thanks for sharing the knowledge! It didn’t occur to me to look into bbPress’s functions. I’ll definitely look into it. I’m trying to figure out how to pass an array of meta, so that each post can individually have several user-selected meta associated with it. I have a feeling I’ll find my answers in functions.bb-meta.php
And just as I thought I was drowning, Brandon Allen’s hand reaches into the dark waters of PHP/MySQL/BuddyPress and pulls me out. Thanks bro! This is exactly what I was looking for.
I’m gong to thoroughly review this and compare it against where I was stuck. This has been a great trial by fire for me, as a developer and a budding WordPress modder. I pray for many more of these experiences.
Thanks again for the assistance Back to coding I go!
Struggling to figure out a way to get a topic’s post_id. I have to approaches I’m considering.
1. Return the latest post’s id (somehow) and then increment it +1 for the object id (to match the new post)
2. Find a way to get the newest post id…Since the post_id is autoincrement, something tells me it’s not meant to be accessed directly. Is there a round-about way of getting it?
Bleh, so frustrating. I know the answer is in front of me somewhere. I’m just not l337 enough to see it yet.
This is inspiring two years eh? I’m just getting my feet wet with BuddyPress, and I’m trying to work towards something similar (a custom theme that I want to build to suit my own specific needs, but then give to the community for free). I look forward to trying out your theme, and who knows maybe build some child themes based on it.
Now back to coding I go.
Okay, I got it semi-working.
1. The function, cg_newmeta, inserts a new record into bp_bb_meta table
2. It gets it’s data from the function, postcatagory.
3. cg_postcatagory() SHOULD get its data from the new topic form (forum.php). However, I haven’t figured out how to do that yet.Basically if I set the variables in cg_postcatagory(), then cg_newmeta() adds them to the bp_bb_meta table. If I try to get it from the New Topic form (in forum.php) via POST, then what happens is that bp_bb_meta gets updated with a record that has object_id set to 0, and meta_value is empty. So that means it’s not getting the relevant info from the New Topic form via POST, right?
Here’s the code so far:
functions.php
`
function cg_newmeta($getid, $getcat) {
global $wpdb, $bbdb;
$id = $getid;
$postcat = $getcat;do_action( ‘bbpress_init’ );
$wpdb->query( $wpdb->prepare( “INSERT INTO $bbdb->meta ( meta_id, object_type, object_id, meta_key, meta_value ) VALUES (%d, %s, %d, %s, %s )”, $catagory->meta_id, ‘bb_cgcat’, $id, ‘cg_catagory’, $postcat ) );
}function cg_postcatagory()
{
$post_id = $_POST;
$postmeta = $_POST;
cg_newmeta( $post_id, $postmeta );
}add_filter(‘bp_after_group_forum_post_new’, ‘cg_postcatagory’);
`
form.php is customized with“
Any ideas why cg_postcatagory() isn’t getting any info from the form?
Okay here’s what I’ve come up with so far. It doesn’t do anything (neither works, nor breaks the website) so I’m definitely doing something wrong. Could somebody point it out?
In functions.php
1. cg_newmeta: take the metadata and add it to the bp_bb_meta table. Code taken from BP Rate Forum Post
2. cg_postcatagory: retrieve the metadata from the user input via a custom forum.phpCode snippet from functions.php
`
function cg_newmeta( $id, $postcat ) {
if ( !$id || !$postcat )
return false;
global $wpdb, $bbdb;do_action( ‘bbpress_init’ );
$catagory = $wpdb->get_row( $wpdb->prepare( “SELECT meta_id, meta_value FROM {$bbdb->meta} WHERE object_type = ‘bb_cgcat’ AND meta_key = ‘cg_catagory’ AND object_id = {$id}” ) );
$wpdb->query( $wpdb->prepare( “REPLACE INTO {$bbdb->meta} ( meta_id, object_type, object_id, meta_key, meta_value ) VALUES (%d, %s, %d, %s, %d )”, $rating->meta_id, ‘bb_cgcat’, $id, ‘cg_catagory’, $postcat ) );
}function cg_postcatagory()
{
$post_id = $_POST;
$postmeta = $_POST;
cg_newmeta( $post_id, $postmeta );
}add_filter (‘bp_after_group_forum_post_new’, ‘cg_postcatagory’);
`
Code snipped from forum.php (relevant bit towards the end)
forum.php
`
`Any suggestions would be helpful. I could particularly use a jump-start on adding data to a table, via a form. I’ll post any finds here.