You've misunderstood how hooks, and PHP, work.
Hooks are not persistent. Each time you visit a page in WordPress the PHP scripts that comprise WordPress all get run. Every time. This includes plugins and the theme's the functions.php file. So when you run add_action()
that queues up a callback function to run whenever the corresponding do_action()
is run. Once everything has run and the page has rendered nothing is remembered. The only persistent information is whatever gets stored in the database, and hook callbacks are not stored in the database.
So, if you try to run add_filter( 'the_content', 'my_function', 10, 1 );
on wp_login
, the my_function()
callback will only be applied to the_content
if the the_content
is displayed for that single request where the user is logged in. This is almost certainly never going to happen because users are typically redirected after being logged in, and that redirect is a separate request.
What you are actually trying to do is filter the_content
if the current user is logged in. The proper way to do that is to check if the user is logged in inside my_function()
and to add your filter for every request:
function my_function( $content ) {
if ( is_user_logged_in() ) {
// Do something with $content.
}
return $content;
}
add_filter( 'the_content', 'my_function' );
Note that add_filter()
is at the 'top level', and not inside another hook. It will run on every request, but the content will only be modified if the user is logged in. Also note that we used is_user_logged_in()
inside the callback function, instead of around add_filter()
. This is because when add_filter()
will be run it hasn't been determined if the user is logged in or not yet.
the_content
filter insidewp_login
filter? Thewp_login
fires after successful user login,wp_content
filters the generated WP content. If you want to limit your changes towp_content
filter for logged in users, useis_user_logged_in()
condition inside the content filter instead (or specific user ID conditions).wp_login
filter), but only then the desired page gets loaded and the content is generated. Yourthe_content
filter callback was however executed on the 'previous' page, not the current one. The approach in the link above should suit your case as well.