Publish blog on front page of a website - php

I'd like to show my latest blog post as a teaser on the front page of my personal website, in a div. Ideally it would even have a word limit -- maybe the first 100 words and then an ellipsis and link to the actual blog along with date and time.
Additional my blog is not attached on my website (so that I can't access it's database). My blog may be saved on like: blogspot.com etc..

I assume that everything is self-coded, so we don't have any abstractions or something. If you run a blog and a website there's probably some database or two. You'll seed to write SQL query that gets latest post from posts table and run it somewhere inside you website's code. If you have that data use field storing post's date, time and full text and extract 100 words [non trivial problem if you have HTML inside] and display them in a proper template file of your website.

Related

What approach to a post based (like a blog) webpage, with separate page to allow users to add a post

I'm currently trying to develop my own website from scratch as I try and teach myself more about web development. I'd like to work out how to have a section on my website that displays blog posts I write, but without having to edit the HTML each time I write a post. I'd like a separate page where someone can write a blog, then when they press submit, it will add it to the website.
I've had a nose about online and a think about it. I'm guessing this is like a content management system, but I'm unsure if this is something I should try and manually code myself or if there's common practice out there for this sort of thing.
My thoughts on how to tackle the problem would be as follows. I'm guessing blog posts will need to be stored in a database. On the page that displays blog posts, it won't contain hard coding of any blog posts, but instead they will be pulled from a database and displayed. So you'd need to write code that will extract records from a database and display the heading / content as you desire, with a separator between posts. And you limit maybe to fetching X amount of posts from the database at a time. Then with regards to the writing of posts, I'd need to make a webpage that you log in to, and write a blog post as if it was a form and submit it to the database via Php or something? I appreciate this may not included a fancy text editor while writing a blog post, mind.
I'm sorry if this isn't the right type of question for stackoverflow. I know I've not presented code for you to help with, but it is about how these things are coded - so I don't start working on something redundant. Thanks for reading.
(FYI: My experiences so far are creating a website that uses HTML, CSS and a small amount of Javascript, using PHP and MYSQL. I've created a log in system, and allowed users to add, edit and delete records via forms. I've displayed information from databases in forms and tables.)
Store everything in database.
Simple way (you can make it complicated later),
table structure:
MainTable
text_id(Primary Key),
used_id(Foreign Key),
approved(allowed to be displayed),
text(text body)
User(You) with access to admin panel.
Admin Panel page to approve texts to be displayed.
select * from MainTable WHERE approved = 0
You have all unapproved texts.
UPDATE MainTable SET approved = 1 WHERE text_id = /this text id/
Now it can be displayed.
Page where you want display texts.
You would have to identify user with user_id to get his texts.
SELECT * from MainTable WHERE user_id = /this user id/ and apporoved = 1
display texts.

WordPress - Get all of current page's queries information

Okay... so here is what I'm trying to do. Basically all of the components that make up any particular page on a Wordpress site, I need to be able to grab the query that WP is running to grab the data for that particular area of the given page. So, if we have page A, let's say it has a top navigation, main content section, and footer. Each of these three areas have content that is pulled from the database. I need a generic way to say on page X I want to know what queries have been ran to generate all of the content pulled from the database and displayed on the page.
I feel like the key is to properly use https://codex.wordpress.org/Class_Reference/WP_Query to get the data I need.
Any thoughts on how I could go about this?

Blog site common approach, display short version of post and full versionn

I'm trying to develop web application (php/mysql), sort of blog site where ~10 posts are listed on front page. Only first 4-5 lines of each post are displayed and when user clicks the title it opens new page with full post displayed.
Users will use TinyMCE to post with very limited functionality, so bold, italics, underline, undo/redo and bullet lists only. No HTML view, no links nor images so nothing fancy.
Now, as two displays of the same posts are involved, so the one on front page where part is displayed only, and full version, I'm not sure if I should:
when user submits post I should clear it up with something like HTMLPurifier and store one full version in database. Then cut first 4-5 lines of it, and clear it with HTMLPurifier again to make sure all tags are closed properly. This is to make sure the text I cut doesn't finish with html tag cut in half but rather is valid (x)html properly closed. I would store this shorten front-page version somewhere in database again. So I would have two versions in database ready, full post version and shorten front-page version. Then when someone accesses the site and displays front page with 10 posts, these just need to be read from database and displayed. No need for reading full version, cutting the top of it, making sure all tags are properly closed etc. etc. for all 10 posts every time someone opens the site. The obvious problem would seem that part of content is doubled in database so full version and short version.
Another option I thought about would be when user submits post, clear it up with HTMLPurifier, and store only full version to database. Then when someone accesses the site, cut top part of each blog post, clear it up with HTML purifier (again!) to make sure its all valid, tags are closed etc. and display on front page. Now the problem seems that HTMLPurifier would have to be run and clean every single time when someone accesses the short version of the post, so e.g. 10x for frontage for each visit and so on.
I used HTMLPurifier above as an example only as I'm sure there are other tools douing same thing so please advice if anything else would be more appropriate in my situation. Also I will probably allow users to display more than 10 posts per page.
Ok, I'm sure there is some common pattern to deal with this sort of site and if so please give me an idea of how it should be done as my thought above seems to have many obvious problems. As always your help is much appreciated.
EDIT: I should have clarified.. Its more like an exercise to me and I wanted to learn how this sort of site should be done. Same concept can be implemented on other sites so advertising, forums etc. anything with posting really. For blogging I sure could use something ready but this time I wanted to learn.
When a user posts a blog entry, all you need to do is insert the entire post into the database. Before inserting, you need to escape/clean-up the post so that nothing harmful is inserted.
When viewing the post you will have two type of views - the excerpt and the full. The excerpt will only be the first 4-5 lines of the post. To get this you need to TRUNCATE the post before outputting it. To do that, you need to create a truncate function that will limit the number of words/characters.
You do not need to insert the post twice ( excert and full ) into the database.
For instance:
// Excerpt Post
echo $postTitle;
echo truncate($postBody); // where truncate is the function you used to trim
// Full Post
echo $postTitle;
echo $postBody;
Hope this gets you started!

Concept & Algorithm: How to record only single URL for widget?

I have created a widget for my web application. User's getting code and just pasting that code in their website and my widget works on their website something like twitter, digg and other social widgets.
My widget is on the basis of post, for a single post (say postid: 234) I am providing single widget, so anyone can embed the widget on their website.
Now I want to know that where all my widget is posted and for which post? for that I have recorded the URL of the site when my widget start (onload) but the problem arises when someone placed the widget in their blog or website's common sidebar. I am recording URL each time and hence if it's in sidebar of a blog then it's recording URL for every post which is creating duplicates.
can anyone help on this? How should I go so that I have only one single record for a widget on a site?
I think doing something like this is a bit tricky. Here are some ideas that pop to mind
You could for example ask the user to input their site's URL when they get the widget, or the widget could track the domain or subdomain, thus giving less URLs.
Just tracking the domain would obviously be problematic if the actual site is domain.com/sitename/, and there could be more than one site under the domain. In that case, you could attempt to detect the highest common directory. Something like this:
You have multiple URLs like this: domain.com/site/page1, domain.com/site/page2, and so on. Here the highest common directory would be domain.com/site.
I don't think that will always work correctly or provide completely accurate results. For accuracy, I think the best is to just ask the user for the URL when they download the code for the widget.
Edit: new idea - Just generate a unique ID for each user. This could be accomplished by simply taking the current timestamp or something, and hiding it into the code snippet the user is supposed to copy. This way you can track the ID itself and any URLs and domains it appears in can be grouped under it.
If you have an ID which doesn't get a hit in say week or something you could remove it from your database, and that way avoid filling it up with unused IDs.
I agree with Jani regarding a unique id. When you dish out the script you'll then be able to always relate back to that id. You are still going to have duplicates if the user uses the same id over and over, but at least you'll have a way of differentiating one user from another. Another useful advantage is that you are now able to, as Jani said, group by the ID and get a cumulative number for all of the instances where that user used the script & id.

counting a Widget?

Hello I've recently added a weather widget for people to add to their site.
Is basically a php page that people can link to.
My question is can I somehow put a counter or something to count how many people are using my widget?
my widget is located here http://www.site.com/widget/ it's in farsi language.
Well, it depends - do you want to know how many people have used your widget in their web site, or how many people have watched web sites that use your widgets? These numbers are completely different.
For the later option, jonstjohn's method would work great: For easy implementation and lots of features I recommend the Google Analytics way - they have really fancy graphs that show a lot of interesting information.
If on the other hand you want to count how many web sites are using your widget then you can do as follows:
Create a table in your database with a varchar column. Put a unique index on that column.
In your code, read the $_SERVER["HTTP_REFERER"] parameter to get the URL that called your widget.
Now you want to strip just the domain part from that URL as a web site would probably put your widget in their template so it will be available in all of their pages. So if you want to count web sites and not pages in web sites, then do something like
$domainParts = explode("/", $_SERVER["HTTP_REFERER"]);
$domain = $domainParts[2];
Now insert the domain you found out into your table. If this web site has already called your widget once then the insert would fail with a unique constraint error - just ignore the error (for example by using "#" on your insert command, like #mysql_query("INSERT INTO...") ).
To know how many web sites are using your widget, simply count the number of rows in the table.
You can place tracking code on the PHP page. Possibilities are:
Google analytics code
Record each time the .php page executes by inserting a row in a database
I'm sure there are others, but those should work.

Categories