Wordpress: Pass $wpdb->insert_id from 1 function to another? - hook

I'm trying to do something simple. I think my code looks sound, but for some reason, the $wpdb->insert_id; keeps being empty? Am I doing something wrong here? Am I doing the passing of the variable correctly? I even tried storing $wpdb->insert_id; in a $_SESSION but it was still empty.
function insert_stuff() {
global $wpdb;
$wpdb->insert('mytable',
array(
'column1' => $_REQUEST['formitem1'],
'column2' => $_REQUEST['formitem2'],
)
);
global $lastid;
$lastid = $wpdb->insert_id;
}
add_action('add_to_cart', 'insert_stuff');
function update_stuff() {
global $wpdb;
global $lastid;
$wpdb->update('mytable', array('column3' => 'newvalue'), array('id' => $lastid), array('%s'), array('%d'));
}
add_action('thank_you_page', 'update_stuff');

Looks like you are missing an array with the $wpdb->insert..
$wpdb->insert(
'table',
array(
'column1' => $var1,
'column2' => $var2,
),
array(
'%d',
'%s',
)
);
Note: %d = numbers.. %s = string..
Best of luck.

Related

change Twig concatenation delimiter

twig uses ~ for its concatenation, is there any way to change this to another symbol?
I know there is a way to change other delimiters, like, the blocks, comments, etc, but I didn't find anything on concatenation. So if someone knows, that would be great!
For the delimeters your talking about, the TwigLexer only define this symbols:
$this->options = array_merge(array(
'tag_comment' => array('{#', '#}'),
'tag_block' => array('{%', '%}'),
'tag_variable' => array('{{', '}}'),
'whitespace_trim' => '-',
'interpolation' => array('#{', '}'),
), $options);
As #DarkBee mentioned, you could define your own operator using https://twig.symfony.com/doc/2.x/advanced.html#operators
You can found already defined operators on this php class to help you define your own:
vendor/twig/twig/lib/Twig/Extension/Core.php class Twig_Extension_Core::getOperators
Your operator would be
class Project_Twig_Extension extends Twig_Extension
{
public function getOperators()
{
return array(
array(),
array(
'~' => array('precedence' => 40, 'class' => 'Twig_Node_Expression_Binary_Concat', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
),
);
}
// ...
}

Buddypress new profile tabs and sub tabs: How to set up url slug properly?

First of all, I’ve seen alot of people trying to do a similar task, which is simply to create a new tab, with sub tabs in the user profile menu. I’ve managed to do this, but I can’t seem to get the url slug to work properly. When I click on the first sub tab, it simply takes me back to the main page for the user profile, and when I click on any additional sub tabs I get 404 errors. I have a feeling I’m missing something pretty simple, and I’ve been trying to learn over the last couple weeks how to make this work without any luck. If someone could help guide me on how to get this working properly, I would very grateful, and I imagine many others would find this post useful in the future.
For the record the main profile tab works properly, but the sub-tabs do not.
Here is the code I currently have in my bp-custom.php file
// My Membership Profile Tab
function profile_new_nav_item() {
global $bp;
bp_core_new_nav_item(
array(
'name' => 'My Membership',
'slug' => 'my-membership',
'default_subnav_slug' => 'extra_sub_tab', // We add this submenu item below
'screen_function' => 'view_manage_tab_main'
)
);
}
add_action( 'bp_setup_nav', 'profile_new_nav_item', 10 );
function view_manage_tab_main() {
add_action( 'bp_template_content', 'bp_template_content_main_function' );
bp_core_load_template( 'template_content' );
}
function bp_template_content_main_function() {
if ( ! is_user_logged_in() ) {
wp_login_form( array( 'echo' => true ) );
}
}
function profile_new_subnav_item() {
global $bp;
bp_core_new_subnav_item( array(
'name' => 'Membership Level',
'slug' => 'extra_sub_tab',
'parent_url' => $bp->loggedin_user->domain . $bp->bp_nav[ 'extra_tab' ][ 'slug' ] . '/',
'parent_slug' => $bp->bp_nav[ 'my-membership' ][ 'slug' ],
'position' => 10,
'screen_function' => 'view_manage_sub_tab_main'
) );
}
add_action( 'bp_setup_nav', 'profile_new_subnav_item', 10 );
function view_manage_sub_tab_main() {
add_action( 'bp_template_content', 'bp_template_content_sub_function' );
bp_core_load_template( 'template_content' );
}
function bp_template_content_sub_function() {
if ( is_user_logged_in() ) {
//Add shortcode to display content in sub tab
echo do_shortcode( '[membership]' );
} else {
wp_login_form( array( 'echo' => true ) );
}
}
// My Billing Profile Tab
function profile_new_subnav_item_billing() {
global $bp;
bp_core_new_subnav_item( array(
'name' => 'Billing',
'slug' => 'extra_sub_tab_billing',
'parent_url' => $bp->loggedin_user->domain . $bp->bp_nav[ 'extra_tab' ][ 'slug' ] . '/',
'parent_slug' => $bp->bp_nav[ 'my-membership' ][ 'slug' ],
'position' => 20,
'screen_function' => 'view_manage_sub_tab_billing'
) );
}
add_action( 'bp_setup_nav', 'profile_new_subnav_item_billing', 20 );
function view_manage_sub_tab_billing() {
add_action( 'bp_template_content', 'bp_template_content_sub_function_billing' );
bp_core_load_template( 'template_content' );
}
function bp_template_content_sub_function_billing() {
if ( is_user_logged_in() ) {
//Add shortcode to display content in sub tab
echo do_shortcode( '[billing]' );
} else {
wp_login_form( array( 'echo' => true ) );
}
}
You may use this:
// define your parent slug
$parent_slug = 'activity';
bp_core_new_subnav_item( array( .........
'parent_url' => $bp->loggedin_user->domain . $parent_slug.'/',
'parent_slug' => $parent_slug,
It works for me.

How do I convert an integer to string in CakePHP?

In my CakePHP app when I try to get data like this:
$this->loadModel('Radio');
$posts = $this->Radio->find('all');
the integers are displayed like strings (in debug) :
'Radio' => array(
'idSong' => '4',
'name' => 'batman',
'title' => 'Batman Theme Song'
),
why? the type is int in the DB. I need integers correctly displayed in my JSON files
Not sure if there's a straightforward solution, but you could change the model data using afterfind
Something like
public function afterFind($results, $primary = false) {
foreach ($results as $key => $val) {
if (isset($val['Radio']['idSong'])) {
$results[$key]['Radio']['idSong'] = (int)$results[$key]['Radio']['idSong'];
}
}
return $results;
}

Infinitescroll to finish after a certain number of posts

I am using Paul Irish's infinitescroll with masonry js on a wordpress site. It is a site with a lot of content. I want infintescroll to stop adding new content when it reaches post number 40 and to give the "No additional items" message at that point. I tried to customize the wordpress loop to only return 40 posts but that did not seem to work.
I thought that maybe one of the options in infinitecroll might do the trick but the infintescroll documentation is very sparse. For example, there is an infinitescroll option in the "loading" init section called "finished: undefined" Is it possible to change that parameter to stop the scrolling after a certain number of content items?
Is there some other obvious way to control when infinitescroll stops loading new content?
Any assistance would be greatly appreciated.
In the Administration -> Settings -> Reading you can set Blog pages show at most to 40.
With code:
Two ways I've done Masonry by numbers like your question I've had success with the following:
limit posts_per_page in your query arguments
$args = array(
'posts_per_page' => 40,
'offset' => 5,
'orderby' => 'post_date',
'order' => 'DESC',
'exclude' => 'none',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts = new WP_Query( $args );
if ( $posts -> have_posts()) {
while ( $posts -> have_posts() ) : $posts->the_post(); {
//do stuff with the posts returned here
}
}
or by incrementing:
$counts = 0 ;
$posts = new WP_Query( $args );
if ( $posts -> have_posts()) {
while ( $posts -> have_posts() ) : $posts->the_post(); {
//do stuff with the posts returned here
$counts = ++;
if($counts == 40) { return }
}
}

Duplicate entry '2' for key 'PRIMARY' on Auto Modeller Update Function in Kohana

Am trying to perform an update and nothing seems to work. It has something to do with my callback I suppose as the update works well when the callback is disabled. This is my try block.
try{
$updatestat=NULL;
$updateresult=NULL;
$id = Arr::get($_POST, 'id');
$scode=trim(Arr::get($_POST, 'stationcode'));
$sname=trim(Arr::get($_POST, 'stationname'));
$dsupdate = new Model_Dstations($id);
$dsupdate->scode = $scode;
$dsupdate->sname = $sname;
$validation = new Validation($_POST);
$validation->rule('scode', array($dsupdate, 'check_updatecheck' ), array( ':validation', ':value',':field',$id ));
$validation->rule('sname', array($dsupdate, 'check_updatecheck' ), array( ':validation', ':value',':field',$id ));
$result['sql']=$dsupdate->save($validation);}
Your code looks like a complete mess.
Try this:
$dsupdate = new Model_Dstations($id);
$validation = new Validation($_POST);
$validation->rule('scode', array($dsupdate, 'check_updatecheck' ), array( ':validation', ':value',':field',$id ));
$validation->rule('sname', array($dsupdate, 'check_updatecheck' ), array( ':validation', ':value',':field',$id ));
if ($validation->check()) {
$dsupdate->scode = $scode;
$dsupdate->sname = $sname;
$dsupdate->save();
}

Resources