How do you write code on wordpress? - wordpress

I have just started experimenting with WordPress, but I have not been able to figure out how to write any code whatsoever. All it gives me is built in blog stuff, and I don't want that. How do I write code on WordPress?

Well, what do you want to achieve?
Are you trying to write code that performs a function that the WordPress core doesn't provide? If so, then you should start by reading Writing a Plugin.
If however you'd like to design your own look and feel, then you'd need to create a theme. The best way to do this would be to write a child theme. Start by reading about Child Themes.

Related

Where to create gutenberg block

This Gutenberg update looks sweet, but it's immediately frustrating trying to follow any of the documentation.
I started with the very simple looking tutorial that is the first result on Google. It has code that looks to make sense for what I'm trying to do here. Great.
Except it says nothing about where to put this code to make it work.
Other tutorials are all about "download our plugin and we'll create one for you" - but I don't want to add that huge nest of code when what I'm really looking for is a fairly simple static block of code I can have users drop into a page.
Back in the day we'd just register a short code in the functions.php file and call it a day.
Can someone explain to me:
Where to put this registration code?
Where to put this JS code it references?
If I'm somehow going about this with a huge misunderstanding of how Gutenberg works?
Thanks
To actually solve this properly would need a couple of chapters of a book... but here's the very condensed answer.
Taking a very quick look at the link you shared, the code should be put in a plugin as there are calls to the function plugins_url(). So you need to first create a plugin. It's not difficult to build a simple plugin but it does take a bit of time getting used to how things need to be registered and how everything is glued together (that's why I said that a proper answer would take a couple of chapters of a book :D).
Once you have the plugin, the code should be within the plugin (or in any PHP file that is included by the plugin) and the JS file should go inside the plugin directory in a folder called step-01
There's a git repo from WordPress with some Gutenberg examples. I haven't looked to hard at them, but It might be a good idea to follow the code in there with the tutorial of how to build a plugin (and the WordPress code reference site opened too :D). If you are going to be developing things for WP, I recommend you to get Zeal or Dash to be able to browse the documentation in an easy and fast way.
Edit
I just found that you can create the scaffolding with wp cli. Wordpress has a tutorial on how to use the wp scaffold block command here.

Add id to elements in wordpress

Im working on a wordpress site and right now a lot of elements has the same id which makes it difficult to customize.
Right now my solution is to add changes to all of the elements which isn't feasible, the difficult part is I am unsure of how to add an ID to the specific element.
Do I do this in the css file or do I edit in php somehow?
Or is it possible to add an ID with some plugin or similar?
There is no short or easy answer to this.
The best way to figure out what's broken in your theme is by looking into the files it contains and how it works. I could not find the Cherry theme that you mentioned in your comment so that I could take a look.
Of course, to understand what's going on you're going to have to know a good amount about how themes are written. I can't suggest a better place to start than the Codex article on theme development. You may get better results (depending on how you learn best) from a web search for something like wp building a theme. I did and I got this and this but I learn best by reading; maybe videos will help you more. Unfortunately, you really won't be able to eliminate this step and it takes time.
Your theme most likely -- if it has been built in the WordPress way -- uses a series of included templates with the get_template() call. As you're looking, this is probably what you want to look for. I remember the first time I dissected a WP theme it was a daunting task and I had already been a very experienced developer for a long time at that point. Don't be discouraged.
Some themes and plugins use actions and filters to allow modification of their content. If your theme does this, modification could be as easy as hooking on a few of the right ones. This will still require figuring out which hooks to attach to.
I may be able to update this answer with something more specific if you point the first 5 or 10 hierarchical children directly below the <body> element in a typical page, like your home page or your blog page.
If this seems too complicated, you're probably best to consider hiring someone who is more familiar with WordPress theme modification to help you out.

Building designs into WordPress and Plugins

I want to build a new design into WP (not make a theme for anyone to use) but I was wondering is there anything specific you have to do to make sure plugins still work?
What about plugins you use on the current theme? Will they automatically work on the new design or do you have to do something for them to continue working?
Edit: To elaborate further, I have a site that currently uses a WP theme, however I want to get a new design created for it and then build that design into WP. I'm just wondering what I need to do to make sure plugins (new and existing) work on the new theme as I will be coding it.
Well the procedure would be like this :-
Create your design, then make the html for the same.
In the existing theme put your html tags properly and add your css in the style.css
Just keep in mind that you don't remove any wordpress functions without knowing it.
And for plugins, dont remove the below functions :-
In header.php there is a function wp_head();, this helps the plugins script to embedd scripts.
In footer.php there is a function wp_footer(); this also helps the plugins to embedd scripts.
Hope these steps help you, let me know if there is any other confusion.

Drupal 6: duplicate theme function in two themes

I have a regular and mobile theme for a site; there are currently a couple of duplicate functions in the template.php of each theme, which do some text munging on the same fields. That is, the markup is the same for these fields in both themes.
How can I impose DRY and only have the logic in one place?
I understand I could make the regular theme inherit the mobile theme, but this doesn't seem like a "proper" solution, since the themes are quite different at the end of the day. Even more so as this would require ex-post-facto manipulation of the preprocess functions, and overriding mobile CSS and JS files - seemingly creating as much work and future WTH as it solves.
Thanks!
I would love to see the proper answer for this.
All i can think of is making a module that holds the duplication of functionality. A library module if you will.
So I'm making this "answer" as a conversation starter.
in: (mobile)template.php
preprocess_page(&$vars){
mymodulename_pagepreprocess($vars,'mobile');
}
(desktop)template.php
preprocess_page(&$vars){
mymodulename_pagepreprocess($vars,'desktop');
}
You can have preprocess-functions in modules as well btw. But it might be nice to send witch template as an argument.(you could however extract this from the global $theme)
How about you make a base theme which holds the functions, and implement both as sub-themes of it.
Or just the mobile theme a sub-theme, and the PC theme is the base?
Creating a sub-theme

Wordpress creating custom theme-Reusability

first approach to CMS and wordpress I'm wondering if there's any predefined html structure and classes/IDs "must-be" reference that I can refer for making my own theme willing to change in the future for another wordpress theme
thanks
Luca
There are a few other 'template' themes that could get you started - if Starkers isn't quite your thing, you might find WP Framework a good alternative. Or - just start stripping down the Twenty Eleven theme to give you a base (which is just what the Starkers theme does, using the Twenty Ten theme as a base).
There's also quite a handy first-time guide on the WordPress Codex around theme development if you'd prefer to start from scratch.
Wordpress doesn't require you to have any specific classes or IDs in your theme in terms of the HTML and CSS, the only things WP needs are things like the wp_head function inside your element on every page. Having said that themes such as Starkers were created to enable developers to have a starting point instead of starting from scratch.
Now the above applies only to whatever code you write, there are however some functions in WP that will return standard code, for instance if you don't specifically create the comment thread code, WP will generate it for you, and that is really the only code that many themes will share.
I would say that if you are intending on making a number of blogging themes for instance, having a set of standard code might be a good idea, for the article pages for example, so that you don't have to re-write code over and over. Aside from that the only code I ever reuse when making themes is the CSS to style comments if I don't hand-code the comments section, this is a good idea as it will save you a lot of time.
Wordpress provide some functions which add CSS classes depending of page type, templete, conditional tags . . .etc.
These functions are body_class() and post_class().
For more info check:
http://codex.wordpress.org/Function_Reference/post_class
http://codex.wordpress.org/Function_Reference/body_class

Resources