Skip to content

Instantly share code, notes, and snippets.

@phoopee3
Created February 16, 2018 19:37
Show Gist options
  • Save phoopee3/57b8c58fe5debab102be7b5c69c90017 to your computer and use it in GitHub Desktop.
Save phoopee3/57b8c58fe5debab102be7b5c69c90017 to your computer and use it in GitHub Desktop.
I want to make an anonymous function that is based off a string i get from a database
// so i have two strings i get from a database, an action, and a callback function name
// let's say the action = 'comment_post', and the function name is 'my_comment_post'
// i want to add an action that is basically add_action( 'comment_post', 'my_comment_post' );
// but i want to do this dynamically for any and all entries i have in the database
// how?
// in my plugin init i had this code, but it wasn't working
// $results->callback would be "my_comment_post"
// $results->hook would be 'comment_post'
${$results->callback} = function () {
// get the current user
$current_user = wp_get_current_user();
if ( 0 == $current_user->ID ) {
// Not logged in.
return;
} else {
// add the point value to their point meta
update_user_meta( $current_user->ID, 'some_key', 'some_value' );
}
};
add_action( "$results->hook", "$results->callback" );
@mauteri
Copy link

mauteri commented Feb 16, 2018

my sample

$results = (object) array(
	'callback' => 'my_wp_head',
	'hook' => 'wp_head',
);

${$results->callback} = function () {
	?>
	<script>alert('here');</script>
	<?php
};

add_action( "$results->hook", ${$results->callback} );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment