peachpiecompiler / peachpie

PeachPie - the PHP compiler and runtime for .NET and .NET Core
https://www.peachpie.io
Apache License 2.0
2.31k stars 201 forks source link

How to run C# code on a word press page #1050

Closed SquareCue closed 2 years ago

SquareCue commented 2 years ago

Hi, thank you in advance for your help on this item.

I'm extremely new to Peach Pie, can you please help me do something simple on it, I already grabbed the GitHub project and ran an instance of MySQL on Docker. So, I have the wp-admin in .net coming up.

Now I have cloned (duplicated) the about a simple page and on that page, I want to run a C# function using Visual Studios 2019. How do I do the simplest C# function that will display on the WordPress page? (ANY function that actually shows up on the word press page is good enough, just need to know how and where I put the code?) (Sorry if this is a super silly question, I've watched some of the demos and they go super in-depth about a lot of things but I need to know the basics first, and I haven't seen any of the demos show this basic thing, but maybe I missed it).

How do I return the User Name that is logged in using C# in Visual studios and display that on the Page in word press?

If I was to do it in PHP it would be something like:

 <?php
        $current_user = wp_get_current_user();

        echo "Username :".$current_user->user_login;
        echo "Username  
    ?>

inside of a shortcode that I call, but I want to fully leverage Visual Studios and have the code on the page run from there, and display on the word press page, how do I do that?

To make my question even simpler, how may I plug in some C# code in VS2019 and have anything display that is called in the Visual Studios display on any page of word press? Thank you very much for your help! I'm still working to figure it out.

(Eventually, what I want to do is call out to a Microsoft SQL Server (NOT MySQL) and use data I have there to display as a table on the word press page.), should be easy enough once I can pass anything to a page from Visual Studios, basically, I just need to know how to hook the C# code to a WordPress page. Once I'm at that point I should be able to do quite a bit with it, but the fist thing I really need to know is how do I pass ANY C# code from Visual Studios 2019 to ANY page or post on WordPress?

Please let me know if my question needs more explanation.

(As an added bonus later once I can get through this part I want to work with BOTH C# and PHP code in VS2019 so I can use both on the same VS page and SEE the output of my code on the WordPress page or post. )

Thank you very much for your help!!

jakubmisek commented 2 years ago

Hi,

I'll have to read the question properly, but short answer might be to create a short code in C#;

https://github.com/iolevel/peachpie-wordpress/blob/a23034ea667a4c2bd31d865f1d43d912e40ae8b7/app/Plugins/ShortcodePlugin.cs#L19

Here you can call whatever C# and return a value. Use the short code in WordPress paga as usual.