phpsword / sword-bundle

Modern WordPress development with Symfony
https://getsword.com
MIT License
124 stars 4 forks source link

Question: Use action / hooks / filter? #3

Open martincodes-de opened 2 years ago

martincodes-de commented 2 years ago

Hey, is it posible to use action / hooks and filters? To, as example, fire some from the symfony part of the application the to the wordpress part?

williarin commented 2 years ago

Symfony and WordPress are merged together, so yes you can call WordPress actions and filters from Symfony.

loicrey commented 1 year ago

Is it possible to do this before Wordpress initialized (which initialize after Symfony) ? Without using services that are triggered by Wordpress events/hooks (https://getsword.com/guide/tutorial/services.html) I need to call some filters with apply_filters and get the result before initialize_services() is called in my child theme. I can't trigger Wordpress Hooks to achieve this because I need to call it when my controller is executed

williarin commented 1 year ago

What do you think of this solution (not tested), assuming you're using a custom Symfony controller that triggers the Wordpress loader:

  1. create a Symfony custom event MyEvent extends Event
  2. create a Wordpress service MyService extends AbstractWordpressService, and in the constructor your inject EventDispatcherInterface and immediately listen to MyEvent. In the listener you store the event data in a property of MyService
  3. in your Symfony controller, you dispatch MyEvent with the data you need, before Wordpress initialization. At this point your data is stored in MyService but Wordpress isn't initalized yet
  4. call apply_filters in MyService->initialize(), using the data you previously stored
loicrey commented 1 year ago

Hi William.

It's a controller who renders a Twig, so controller is not triggering the Wordpress loader I think

(In my case, Wordpress is only used for back-end as a CMS and I disabled Wordpress front-end, front-end is rendered with Symfony and I directly gather the data via the Wordpress sql database)

williarin commented 1 year ago

Hi Loïc,

From what I understand, you want to make Wordpress render some html or some output in your controller using apply_filters, then print it in your Twig template.

Some possibilities:

Not sure how to do it otherwise as Wordpress can't be unloaded... So you must find a way to isolate the rendering process.