2

Assuming that my site uses a database through the web service to fetch content, style and other things for my site. Can we make this connection in a way provided by the CMS? A plugin does that? Should we do it ourselves?

Thanks for answers.

9
  • 1
    A "web service" could be literally anything. What are you actually trying to do? Apr 22, 2022 at 8:17
  • Hi, I have a database. I have a web service that has database management functions to handle the content and users I have. I would like to use a Wordpress site to display the content. I have tried to program a widget that would do this, but it is not really appropriate. The problem with the widget is its area and the fact that it doesn't affect the content of the site in general but only itself. I would like to know if a widget/plugin can change the structure of the site and its content. If so, how can I do it?
    – Jonathan
    Apr 22, 2022 at 8:21
  • 1
    Sorry but I'm having trouble understanding what you're trying to do, because if the content and users are external, I don't see what the point of using WordPress is? WordPress has its own database for content and users and isn't designed to work with arbitrary external services. You would have to implement any importing or syncing of that data to WordPress yourself entirely from scratch and doing that would be well beyond the scope of a single question on this site. Apr 22, 2022 at 9:44
  • Maybe you are right, I just saw your profile, how long have you been coding with Wordpress? What can you tell an amateur like me about the possibilities of Wordpress plugins?
    – Jonathan
    Apr 22, 2022 at 14:19
  • 2
    Plugins can do just about anything. It may help to read up on developer.wordpress.org/plugins/intro and get a basic understanding of the CMS. Sometimes it does make sense to integrate multiple sites, but if you have an existing setup of content and users, it more likely makes sense to either keep that or else migrate everything to WP and have WP manage everything moving forward. What benefits of WP are you trying to gain?
    – WebElaine
    Apr 22, 2022 at 18:49

2 Answers 2

2

Yes, you can write php code, potentially in the form of a plugin, to access a remote web service.

Presumably you want to use a web service accessed via https (or http). WordPress provides the wp_remote_request() function to do that. You can call it from your php code (in your plugin). When your server runs that function, it will retrieve a result from your web service. You can then use it as needed. Notice this is server-side code, not client-side.

Or you can use the simpler wp_remote_post() or wp_remote_get() functions if they work for your web service.

Edit You can write a plugin, but that takes software-engineering work to make it integrate cleanly with your site.

You can use the Code Snippets plugin to insert a little chunk of php. That's ok for development and experimentation, but a sizable security risk in production (or in dev for that matter).

Some suggest editing your theme's functions.php file to add a bit of code. But I don't suggest that approach: theme updates will make your changes vanish.

You didn't mention what you want to do with the result you get back from your webservice, so it's hard to recommend which hook you should use to invoke your code.

6
  • My plugin will allow to add to the current theme a search widget that will use a specific function of the webservice. (For the moment I'm only focusing on this function of the webservice) The goal is, that this widget fetches the information from the client in the webservice and displays it correctly on the site. I tried to replace the widget with the information but I was restricted by the widget area. I also tried to use the wp_insert_post function to add the information as articles (this result is what I'm looking for, but I don't like the way I'm doing it)
    – Jonathan
    Apr 25, 2022 at 12:08
  • Aside: I'd generally recommend against using "code snippet" plugins - executing arbitrary code stored in the database is a minor convenience at the cost of a lot of unnecessary risk to security.
    – bosco
    Apr 25, 2022 at 16:15
  • 1
    A good point, @bosco . It's a tough call how to answer a question where we can't tell the questioner's expertise level. Fortunately, in comments they say they have a plugin in the works.
    – O. Jones
    Apr 25, 2022 at 18:13
  • Hello, thank you for your answers, to tell you my level, I discovered Wordpress 6 months ago. I learned to understand themes, plugins, site customization, hooks, development with Wordpress. I saw tutorial videos, past conferences, documentations coming from Wordpress mainly. We can say that I am a beginner/amateur in programming with Wordpress.
    – Jonathan
    Apr 26, 2022 at 7:23
  • Then to talk about the 'Code Snippets' plugin, I read in the doc that it "replaces" the functions.php of the theme by adding functions in it. But if it adds functions in the theme, will it work for all themes? My solution with my plugin should work on all themes or as many as possible.
    – Jonathan
    Apr 26, 2022 at 7:23
1

The answer to the current question is found. Thank you all for your answers.

The answer to the question is 'yes'. We can use an external webservice/API in the Wordpress code. We can use for this, wp_remote_request to call through an URL. For my part, I did otherwise because my webservice is a special one, so I created a SoapClient class in the files of my plugin to make the connection and the function calls.

To provide a correct display of the webservice, I decided to use 2 widgets. 1 for the search and 1 for the display. I still have some problems with the display with the web page but that will be another issue. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.