wordpress do_action after loop - wordpress

i am working on a wordpress(3.3.1) site with a buddypress plugin. i am new to wordpress and i have some knowledge gaps when it comes to hook actions, but i have been programming in php for several years.
my question comes in reference to adding some of my logic after a wordpress/buddypress loop. in particular, i would like to add my logic at this point in a call:
<?php do_action( 'bp_after_profile_edit_content' ); ?>
as i said, my understanding is limited, because i thought i could just add a function in the functions.php file with the name "bp_after_profile_edit_content" that could just echo whatever i wanted. i have found out that is not the case.
thus, i would like to ask for some clarification on the topic. maybe a useful link. i am still trying to understand the codex as far as wp actions go.

I noticed You've figured it out, but i'll answer it in case someone else finds this in the future with a similar issue. :)
function stackoverflow_example_action() { //unique function name
echo 'This text will echo'; //something to do.
} //lets end the function...
add_action( 'bp_after_profile_edit_content', 'stackoverflow_example_action' ); //now we need to add_action so that WordPress knows where to add it.
That will echo the text when 'bp_after_profile_edit_content' is being done.
Add Action: http://codex.wordpress.org/Function_Reference/add_action

Related

A Poll That Is Auto Created For Each Post in Wordpress

I'm looking to have a poll at the end of each of our posts that the viewer can vote on. I don't want to have to manually create a poll for each post.
The only plugins I can find are star ratings. We're looking for a custom poll that asks something like "Would you consider this article: Pro This, Anti This or Right in the Middle". Otherwise I have to create a unique poll for each post and manually insert. We're planning on having dozens of new posts a day.
I've been searching everywhere and cannot find anything like this. I'm hoping someone has a recommendation.
Like I wrote in the comment, you can easily achieve what you want in several ways. For example :
Create child theme ( create custom single.php or post.phpor any other alternative )
Create a child theme with a custom function in functions.php ( see below )
create own plugin with custom function and use the_content filter ( see below )
function whateverContentYouWant($content) {
if(!is_feed() && !is_home()) { // your conditions if any
$content.= "Enjoyed this article?"; // add your content here
}
return $content;
}
add_filter ('the_content', 'whateverContentYouWant');
That being said, I am not sure this site is the best place to search plugin recommendations. better if you have a more specific code-related issues ( with examples )

Find from where the data comes to "the_content()" function

This might look an ordinary question though I'm stuck in it. I'm new to the wordpress. I've bought a wordpress theme and I'm trying to edit some pages as I want. Now I want to edit the default post page where I've already started editing the "single.php" and "post-format.php" files. I want to know from where or how "the_content()" function gets data?
Since I want some html part of the page to be removed though that html part comes through a "the_content" function. Therefore, I'm unable to remove that part without getting rid of "the_content" function. But I can't get rid of "the_content" function because the very same function calls some important part too.
Hope you guys can help!
From Where
As a function get_the_content() retrieve the post content (Generally Used in a Loop) from database and prints on the frontend.
And again the_content() as filter controls how you show the post content.
How
Dead simple answer by WordPress API. You need to understand WordPress Database_API for in-depth understanding.
Frequently Used by
wp-includes/plugin.php: apply_filters()
wp-includes/post-template.php: get_the_content()
wp-includes/post-template.php: the_content
Usage Case: (from Codex)
post_password_required()
get_the_password_form() if post_password_required() fails
wp_kses_no_null() while processing the tag.
balanceTags()
get_permalink()
global: $id, $post ,$more, $page, $pagesm, $multipage, $preview, $pagenow
Ref:
- Filter the_content() | Functionthe_content()
Optional: Offline WP Codex Docs Search/Browser for Windows/OSx

WordPress plugins - don't print the comments on a post page

I'm making a plugin where I want the comments in a single post page not to be printed at all. I need to query the database myself and print my own html with the results.
How can I make WordPress to not print the comments, without disabling them?
Thank you
EDIT:
as a suggestion, I am using:
apply_filters('comments_template', array($this,'comments_template'), 10, 1);
function comments_template($template){
$template = '';
return $template;
}
nothing happens, what am I doing wrong?
You could use the comments_template filter to make WordPress use your plugin's template file rather than the current theme's.
EDIT: based on your edited code: unfortunately you need to have an actual file, the path to which you return in $this->comments_template()...
class MyPlugin{
//add the filter somewhere...
function comments_template($template){
return dirname(__FILE__) . "/my_comments_template.php";
}
}
The file plugin_dir/my_comments_template.php must exist, otherwise WP falls back on the default theme's comments.php. See wp-includes/comment-template.php on lines 911-917.
In plugin_dir/my_comments_template.php you could call `MyPlugin::do_comments() or something like that. I don't know any other way around this. Let me know if you find a better way.
Cheers, Chris

WordPress functions.php: how to apply update_option()?

I'm trying to set the default Image Link URL for my WP users so that it doesn't include the url link as a default. I've done some research, and I know the function is in the wp-admin/options.php:
update_option('image_default_link_type','file');
Rather than mess with the core files, I'd like to put this into the functions.php, but never know the proper way to implement stuff like this! This is what I have so far in my functions.php:
<?php
update_option('image_default_link_type','none');
?>
This obviously doesn't work: it needs the proper setup! What is the correct way to implement this in functions.php?
Also: I'd like to know the strategy for figuring out how to implement functions like this in the future by myself? For example, I never know whether or not I'm supposed to use add_filter or do_action, and how I need to pass the parameters. I've yet to find a book or post out there that explains this very well, and can show me by example. Any good leads on this would be awesome too!
Start with the Wordpress codex. Visit the plugin API (which is really what you are doing) that explains Hooks, Actions and Filters. Then see the Action Reference which provides your list of hooks.
Here you will find the hook update_option_OPTIONNAME. Description from codex:
Runs after a WordPress option has been update by the update_option
function. Action function arguments: old option value, new option
value. You must add an action for the specific options that you want
to respond to, such as update_option_foo to respond when option "foo"
has been updated.
Adding code from asker's comment:
function inventory_linkurl_setting() {
update_option('image_default_link_type','none');
}
add_action('admin_init', 'inventory_linkurl_setting'); ?>

How to filter Wordpress posts using a hook in a plugin?

I'm creating a Wordpress plugin and, being a newbie in the development on this platform, I'm stuck on this problem.
I'd like to have posts in the loop filtered by categories, defined by the user through an admin page. I would actually like to be able to modify query_post() parameters in the plugin, but the only trick I found is to re-run the query_post() with my user-defined criteria, thing that I would like to avoid.
Also, due to the plugin nature, I think it make no sense to modify the theme's template.
I'm sure the solution is evident, but can't find it!
I thought there was a nicer solution, but this is how I finally solved it:
add_filter ( 'query_vars', 'myplugin_filter_posts');
function myplugin_filter_posts( $content )
{
//WP's query handler
global $wp_query;
//The id of the category whose posts I'd like to show
$catId = 1;
$result = $wp_query->query( 'cat='.$catId );
return $content;
}
If you tips for a better solution, please share :)

Resources