I want to use the WordPress JSON api to fetch content for a mobile app. I can iterate over the posts I receive - and I notice there is a "Rendered" field in the posts. I want to display that to the mobile user. Generally in a scrolling list view. However that rendered content is missing all styling CSS information. I'd like to embed that JSON API "rendered" content in web-views without fetching each of the whole posts like www.example.com/customtype/slug1 in WebView1 then the same for slug2 in WebView2 etc. I mean - I already fetched the content part - just missing the styles. Mobile data is precious :)
Is there a way to fetch the CSS information for a post in the active theme via the REST API? I tried caching style info in a global var as discussed in this SO answer (which I verified in debug). Then adding a custom REST endpoint to fetch the stuff I stored in that global variable. This almost works. The endpoint works. The data is cached after I display any post. However the global var in the child theme required to make this work appears to not be in the same scope as the plugin where I'm putting my rest to mobile stuff (that same global is always null/empty in the plugin).
My thoughts are now to make a custom post display in my child theme that just emits only the wp_head() stuff and I then fetch&cache that in the app - but that seems awkward. But I'm new to WP and PHP so - here we are. Thanks for any help!
Related
I have to add the custom Post id for the custom post type. can anyone help me?
Check screen short for more details. http://prntscr.com/mo3eyu
This is a work around that works.
Background info:
We maintain a few almost identical web pages with different branding. The code repo is shared across these. The developers hard coded page IDs into the PHP in the past. I was asked to add new pages. After adding new pages on each website, I noticed the IDs were all randomly generated. This was breaking PHP code's logic. So, I needed to edit these IDs just like OP.
The work around:
In WP web admin page, there's an export / import functionality under tools menu. The new pages should be created in one site, then exported / imported onto other websites. This way, page IDs don't get created randomly. It copies the IDs from the exported page. So you can have same IDs on all pages.
PS:
Not sure why the past developers decided to hard-code page IDs in the code. This shouldn't have been the case to start with. When you don't have much time to refactor everything, stack overflow always comes to help.
IDs determine post record storage in database, changing them directly would be highly prone to breaking things. For example extensions often store IDs as a way to refer to specific post and do something with it.
The simple way to change to some ID would be to just create a new post and copy data over (through admin or with code either).
Post ID of a post also reflect in tables other than Post table, like post comments table. Changing post ID will disturb the relationship of post table with other tables, so it is not advisable.
I'm trying to automate posting entries to a blog every time a specific action happens on my website.
I've been looking at the Wordpress documentation but I've only seen that I can put as the structure of the post. I would be interested in creating entries with text and images. Would it be possible to do this?
The text and images would be taken from the web page.
I'm using php with Laravel and the blog is in wordpress.
You can send a POST request to the "post" endpoint to POST a new post (no pun intended) (docs).
Although the content parameter is said to be an object in the docs, you can pass a HTML string to it like <h1>Some cool event happened</h1><p>See this picture</p><img src="https://some-path-to-picture.com/picture.png">
It seems you can even add images along with your content to be hosted on your wordpress site (see this Github question)
I am using php to pull in posts from a WP site using the REST API, and it gives you both options to use full content, and excerpt. Although the excerpt is just too short for what I am using it for. I also don't want to change the global excerpt length, just for this one project using the REST API. Is there anyway to change the excerpted length via REST API and not effect the rest of the website?
The only thing I can think of is truncating the full content via small script when pulling in the data from the REST API and creating my own "read more" link using other variable that are accessible like source_url.
I am new to REST API in wordpress, but I don't see anything related to it in the official documentation, so wasn't sure if someone knew a good trick, or if I'll just have to go the manual route. Thanks!
The excerpt length is just used to limit the length of the output in your template. So you should handle it's length in your WordPress template, more specifically in your functions.php not in your REST API.
See here for more info:
http://smallenvelop.com/limit-post-excerpt-length-in-wordpress/
Unfortunately there's no way to set the excerpt length for your pulling request, like $api-getposts(300) or something like that, since the excerpt is strongly tied to your WordPress site.
However, you could register an api-callback on your WP-site, which sets the excerpt to a higher value, pull the data, and reset it.
https://codex.wordpress.org/Plugin_API/Filter_Reference/excerpt_length
Just wrap this function in a register_rest_route
So your request would look something like this
$api->setExcerpt(300);
$api->getData();
$api->setExcerpt(100);
I am new to codeigniter. I have created a page that has lot of contents on single page. I want to load those contents in a fancy way like it should load some contents and as we scroll down and reach at the end of the page then it should display some sort of loading feature and load other contents and so on..
The example of such websites can be seen in social networking sites such as Facebook, Twitter or Quora.
I have thoroughly searched for it and unable to find anything suitable.
Any help will be appreciated.
Thanks
What you need is a "Infinite scroll" plugin. If you google that you can find some plugins like infinite-scroll and jScroll
The basic functionality of these plugins is to "monitor" the users position and, at the end of just before the page content ends, the plugin makes an $.ajax request to get more results and add those results to the dom.
I have a website made with WP (a customer passed me). He want to see in one of the pages, a list of product categories, and to get the list of this product categories, I have to send a request to an API in another website. The site will response with an XML that contain the categories. To make the request I will use some PHP library. After the response is arrived, I want to show those categories in the page of my site of my customer.
I have followed the first answer here to call a php file before rendering a template, but imagine that I want to pass a variable (product categories) from the php to to the template. How can I do that?
You will probably want to cache the results of the XML that comes back to you (unless it is truely dynamic), and possibly store it in a table. You can decide how often you need to refresh the cache. (This will protect your site and their API from DOS attacks, or even just high volume).
Once you have your data stored in a table, your template can simply retrieve it.
This pattern will reduce the coupling between the two parts of your solution, and make things a bit easier to build / debug.