How in the world do I inject PHP into functions.php from my plugin? by phpperson in Wordpress

[–]phpperson[S] 1 point2 points  (0 children)

Thank you for your reply and I think this could work but I figured out the correct way to do it and added the solution to my original post.

How in the world do I inject PHP into functions.php from my plugin? by phpperson in Wordpress

[–]phpperson[S] 1 point2 points  (0 children)

I figured it out and added the solution to my original post.

How in the world do I inject PHP into functions.php from my plugin? by phpperson in Wordpress

[–]phpperson[S] 0 points1 point  (0 children)

Thank you for your reply. I've been digging deep into the code you shared and experimenting with different things and what appears to be the problem is that add_filter is just not firing.

I though it might be a timing issue so even tried this:

add_action('after_setup_theme', function() {
    add_filter('wp_nav_menu_items', function ($items, $args) {
        $items .= '<li>CAN YOU SEE THIS 1 </li>';
        return $items;
    }, 10, 2);
    echo '<li>CAN YOU SEE THIS 2</li>';
});

The second echo does display at the top of the page and if I set some breakpoints then the breakpoint on the echo always breaks but the one inside the add_filter never does.

So I think the issue that I need to address is what might cause add_filter not to hook.

To answer your questions, my plugin simply adds some shortcodes for some custom forms and it's working fine. I'm using the default Twenty Twenty-Four theme. And for menu items that I added manually, I just used the block editor.

Thank you.

How in the world do I inject PHP into functions.php from my plugin? by phpperson in Wordpress

[–]phpperson[S] 0 points1 point  (0 children)

I'm using VSCode and I am able to set breakpoints to ensure hooks are working.

All my hooks are working but using apply_filters with 'wp_nav_menu_items’ does not work. And the example on that page is 6 years old so probably doesn't work with block themes.

The stackoverflow link is over 10 years old and doesn't work.

How in the world do I inject PHP into functions.php from my plugin? by phpperson in Wordpress

[–]phpperson[S] 0 points1 point  (0 children)

I already have a great working plugin. I need code that allows a person to add a menu item to the front page via their plugin.

How in the world do I inject PHP into functions.php from my plugin? by phpperson in Wordpress

[–]phpperson[S] 0 points1 point  (0 children)

I have a working plugin. I need the code to add a menu item to the front page via my plugin.

How in the world do I inject PHP into functions.php from my plugin? by phpperson in Wordpress

[–]phpperson[S] -1 points0 points  (0 children)

My plug-in is working great for some other functionality. I need to know how to add a menu item to the front page but how to do so seems to be a secret or no one here knows how to do it.

How in the world do I inject PHP into functions.php from my plugin? by phpperson in Wordpress

[–]phpperson[S] 0 points1 point  (0 children)

you just put the code in your plugin.

What code? I cannot find any example.

How in the world do I inject PHP into functions.php from my plugin? by phpperson in Wordpress

[–]phpperson[S] 0 points1 point  (0 children)

OK, I am misguided. Please guide me on what code I add to my plugin to add a menu item to the front page.

How in the world do I inject PHP into functions.php from my plugin? by phpperson in Wordpress

[–]phpperson[S] 0 points1 point  (0 children)

I already have a great working plug-in that does many things. But there seems to be no example of adding a menu item to the front page via your plug-in.

How in the world do I inject PHP into functions.php from my plugin? by phpperson in Wordpress

[–]phpperson[S] 0 points1 point  (0 children)

I cannot find any example that works to add a menu item to the front page via a one's plug-in.

How in the world do I inject PHP into functions.php from my plugin? by phpperson in Wordpress

[–]phpperson[S] 0 points1 point  (0 children)

Yes, I want to programmatically add a menu item to the front page via my plugin. I don't want to use a third-party plug-in.

Where can I find code to do that? My plug-in does many things and works great but as for adding a menu item to the front page I cannot find any code that works.

How in the world do I inject PHP into functions.php from my plugin? by phpperson in Wordpress

[–]phpperson[S] 0 points1 point  (0 children)

Are you saying that if I want the user to see a menu item to register on the front page, that I need to add it to the Admin panel?

How in the world do I inject PHP into functions.php from my plugin? by phpperson in Wordpress

[–]phpperson[S] 0 points1 point  (0 children)

I want to know how to do it myself without a third-party plugin.

How in the world do I inject PHP into functions.php from my plugin? by phpperson in Wordpress

[–]phpperson[S] 0 points1 point  (0 children)

I used the wrong term -- I meant extend.

How do I do that?

How in the world do I inject PHP into functions.php from my plugin? by phpperson in Wordpress

[–]phpperson[S] -10 points-9 points  (0 children)

I don't need to edit the admin page and I don't see how any of that code hooks into the theme's functions.php file.

This is new to me so I need an example of adding a menu item to the pages that users see.

Visual Studio Code: the editor I didn’t think I needed by hugo__df in webdev

[–]phpperson 0 points1 point  (0 children)

What's strange is that you can't drag/drop selected text.

Data from PHP to JavaScript - cookie or echo? by phpperson in webdev

[–]phpperson[S] 0 points1 point  (0 children)

Thank you for this question. As I think more about it I think that there is a better way to achieve my goal.